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) <noreply@anthropic.com>
This commit is contained in:
2026-03-15 14:22:54 +01:00
parent 502e2d0387
commit 9f72840722
+9
View File
@@ -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(