|
|
|
|
@ -105,15 +105,38 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
|
"""
|
|
|
|
|
Global exception handler for Fastapi application.
|
|
|
|
|
"""
|
|
|
|
|
import uuid
|
|
|
|
|
error_id = str(uuid.uuid1())
|
|
|
|
|
request_info = get_request_context(request)
|
|
|
|
|
|
|
|
|
|
# Store error_id in request.state for middleware/logging
|
|
|
|
|
request.state.error_id = error_id
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, RateLimitExceeded):
|
|
|
|
|
return _rate_limit_exceeded_handler(request, exc)
|
|
|
|
|
log.warning(
|
|
|
|
|
f"Rate limit exceeded | Error ID: {error_id}",
|
|
|
|
|
extra={
|
|
|
|
|
"error_id": error_id,
|
|
|
|
|
"error_category": "rate_limit",
|
|
|
|
|
"request": request_info,
|
|
|
|
|
"detail": str(exc.description) if hasattr(exc, "description") else str(exc),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
status_code=429,
|
|
|
|
|
content={
|
|
|
|
|
"data": None,
|
|
|
|
|
"message": "Rate limit exceeded",
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
"error_id": error_id
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, RequestValidationError):
|
|
|
|
|
log.error(
|
|
|
|
|
"Validation error occurred",
|
|
|
|
|
log.warning(
|
|
|
|
|
f"Validation error occurred | Error ID: {error_id}",
|
|
|
|
|
extra={
|
|
|
|
|
"error_id": error_id,
|
|
|
|
|
"error_category": "validation",
|
|
|
|
|
"errors": exc.errors(),
|
|
|
|
|
"request": request_info,
|
|
|
|
|
@ -122,17 +145,18 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
status_code=422,
|
|
|
|
|
content={
|
|
|
|
|
"data": None,
|
|
|
|
|
"data": exc.errors(),
|
|
|
|
|
"message": "Validation Error",
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
"errors": exc.errors(),
|
|
|
|
|
"error_id": error_id
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, (HTTPException, StarletteHTTPException)):
|
|
|
|
|
log.error(
|
|
|
|
|
"HTTP exception occurred",
|
|
|
|
|
f"HTTP exception occurred | Error ID: {error_id}",
|
|
|
|
|
extra={
|
|
|
|
|
"error_id": error_id,
|
|
|
|
|
"error_category": "http",
|
|
|
|
|
"status_code": exc.status_code,
|
|
|
|
|
"detail": exc.detail if hasattr(exc, "detail") else str(exc),
|
|
|
|
|
@ -146,19 +170,16 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
|
"data": None,
|
|
|
|
|
"message": str(exc.detail) if hasattr(exc, "detail") else str(exc),
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
"errors": [
|
|
|
|
|
ErrorDetail(
|
|
|
|
|
message=str(exc.detail) if hasattr(exc, "detail") else str(exc)
|
|
|
|
|
).model_dump()
|
|
|
|
|
],
|
|
|
|
|
"error_id": error_id
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, SQLAlchemyError):
|
|
|
|
|
error_message, status_code = handle_sqlalchemy_error(exc)
|
|
|
|
|
log.error(
|
|
|
|
|
"Database error occurred",
|
|
|
|
|
f"Database error occurred | Error ID: {error_id}",
|
|
|
|
|
extra={
|
|
|
|
|
"error_id": error_id,
|
|
|
|
|
"error_category": "database",
|
|
|
|
|
"error_message": error_message,
|
|
|
|
|
"request": request_info,
|
|
|
|
|
@ -172,14 +193,15 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
|
"data": None,
|
|
|
|
|
"message": error_message,
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
"errors": [ErrorDetail(message=error_message).model_dump()],
|
|
|
|
|
"error_id": error_id
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Log unexpected errors
|
|
|
|
|
log.error(
|
|
|
|
|
"Unexpected error occurred",
|
|
|
|
|
f"Unexpected error occurred | Error ID: {error_id}",
|
|
|
|
|
extra={
|
|
|
|
|
"error_id": error_id,
|
|
|
|
|
"error_category": "unexpected",
|
|
|
|
|
"error_message": str(exc),
|
|
|
|
|
"request": request_info,
|
|
|
|
|
@ -191,10 +213,9 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
|
status_code=500,
|
|
|
|
|
content={
|
|
|
|
|
"data": None,
|
|
|
|
|
"message": str(exc),
|
|
|
|
|
"message": "An unexpected error occurred",
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
"errors": [
|
|
|
|
|
ErrorDetail(message="An unexpected error occurred.").model_dump()
|
|
|
|
|
],
|
|
|
|
|
"error_id": error_id
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|