From 9f72840722660edcc5d2699980340b7ef938d1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= Date: Sun, 15 Mar 2026 14:22:54 +0100 Subject: [PATCH] fix: better error handling for chat AI errors Catch all exceptions (not just ValueError) and return meaningful error messages from OpenAI API errors instead of generic 500. Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/app/api/routers/chat.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/app/api/routers/chat.py b/backend/app/api/routers/chat.py index 9d714bf..3ca059b 100644 --- a/backend/app/api/routers/chat.py +++ b/backend/app/api/routers/chat.py @@ -97,6 +97,15 @@ async def send_message( except ValueError as exc: # AI not configured raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(exc)) + except Exception as exc: + error_msg = str(exc) + # Extract meaningful error from OpenAI exceptions + if hasattr(exc, 'message'): + error_msg = exc.message + elif hasattr(exc, 'body') and isinstance(exc.body, dict): + error_msg = exc.body.get('error', {}).get('message', error_msg) + logger.error("Chat error: %s", error_msg) + raise HTTPException(status_code=500, detail=f"AI error: {error_msg[:500]}") except Exception as exc: logger.exception("Chat error for user %s", user.id) raise HTTPException(