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:
@@ -97,6 +97,15 @@ async def send_message(
|
|||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
# AI not configured
|
# AI not configured
|
||||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(exc))
|
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:
|
except Exception as exc:
|
||||||
logger.exception("Chat error for user %s", user.id)
|
logger.exception("Chat error for user %s", user.id)
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
|||||||
Reference in New Issue
Block a user