Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
dca87f5
1
Parent(s): 2f2615b
Claude Code: add HTTPException handlers to error_handlers.py
Browse filesAdded FastAPI exception handlers (http_exception_handler and
generic_exception_handler) to properly handle HTTPException and
generic exceptions in the FastAPI app.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- error_handlers.py +47 -0
error_handlers.py
CHANGED
|
@@ -7,6 +7,53 @@ import json
|
|
| 7 |
import os
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import Dict, Any, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# Configuration paths
|
|
|
|
| 7 |
import os
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import Dict, Any, Optional
|
| 10 |
+
from fastapi import HTTPException, Request
|
| 11 |
+
from fastapi.responses import JSONResponse
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
|
| 15 |
+
"""
|
| 16 |
+
FastAPI exception handler for HTTPException.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
request: The incoming request.
|
| 20 |
+
exc: The HTTPException that was raised.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
JSONResponse with error details.
|
| 24 |
+
"""
|
| 25 |
+
return JSONResponse(
|
| 26 |
+
status_code=exc.status_code,
|
| 27 |
+
content={
|
| 28 |
+
"error": True,
|
| 29 |
+
"message": exc.detail,
|
| 30 |
+
"status_code": exc.status_code,
|
| 31 |
+
"agent": "cain"
|
| 32 |
+
},
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
async def generic_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
| 37 |
+
"""
|
| 38 |
+
FastAPI exception handler for generic exceptions.
|
| 39 |
+
|
| 40 |
+
Args:
|
| 41 |
+
request: The incoming request.
|
| 42 |
+
exc: The Exception that was raised.
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
JSONResponse with error details.
|
| 46 |
+
"""
|
| 47 |
+
return JSONResponse(
|
| 48 |
+
status_code=500,
|
| 49 |
+
content={
|
| 50 |
+
"error": True,
|
| 51 |
+
"message": "Internal server error",
|
| 52 |
+
"detail": str(exc),
|
| 53 |
+
"type": type(exc).__name__,
|
| 54 |
+
"agent": "cain"
|
| 55 |
+
},
|
| 56 |
+
)
|
| 57 |
|
| 58 |
|
| 59 |
# Configuration paths
|