|
|
|
@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional
|
|
|
|
from asyncpg.exceptions import DataError as AsyncPGDataError
|
|
|
|
from asyncpg.exceptions import DataError as AsyncPGDataError
|
|
|
|
from asyncpg.exceptions import PostgresError
|
|
|
|
from asyncpg.exceptions import PostgresError
|
|
|
|
from fastapi import FastAPI, HTTPException, Request
|
|
|
|
from fastapi import FastAPI, HTTPException, Request
|
|
|
|
|
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
@ -14,6 +15,7 @@ from sqlalchemy.exc import DataError, DBAPIError, IntegrityError, SQLAlchemyErro
|
|
|
|
|
|
|
|
|
|
|
|
from src.enums import ResponseStatus
|
|
|
|
from src.enums import ResponseStatus
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ErrorDetail(BaseModel):
|
|
|
|
class ErrorDetail(BaseModel):
|
|
|
|
field: Optional[str] = Field(None, max_length=100)
|
|
|
|
field: Optional[str] = Field(None, max_length=100)
|
|
|
|
@ -56,7 +58,7 @@ def get_request_context(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
"endpoint": request.url.path,
|
|
|
|
"endpoint": request.url.path,
|
|
|
|
"url": request.url,
|
|
|
|
"url": str(request.url),
|
|
|
|
"method": request.method,
|
|
|
|
"method": request.method,
|
|
|
|
"remote_addr": get_client_ip(),
|
|
|
|
"remote_addr": get_client_ip(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -102,30 +104,63 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
Global exception handler for Fastapi application.
|
|
|
|
Global exception handler for Fastapi application.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
request_info = get_request_context(request)
|
|
|
|
request_info = get_request_context(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, RateLimitExceeded):
|
|
|
|
if isinstance(exc, RateLimitExceeded):
|
|
|
|
_rate_limit_exceeded_handler(request, exc)
|
|
|
|
return _rate_limit_exceeded_handler(request, exc)
|
|
|
|
if isinstance(exc, HTTPException):
|
|
|
|
if isinstance(exc, RequestValidationError):
|
|
|
|
logging.error(
|
|
|
|
log.error(
|
|
|
|
f"HTTP exception | Code: {exc.status_code} | Error: {exc.detail} | Request: {request_info}",
|
|
|
|
"Validation error occurred",
|
|
|
|
extra={"error_category": "http"},
|
|
|
|
extra={
|
|
|
|
|
|
|
|
"error_category": "validation",
|
|
|
|
|
|
|
|
"errors": exc.errors(),
|
|
|
|
|
|
|
|
"request": request_info,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
|
|
status_code=422,
|
|
|
|
|
|
|
|
content={
|
|
|
|
|
|
|
|
"data": None,
|
|
|
|
|
|
|
|
"message": "Validation Error",
|
|
|
|
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
|
|
|
|
"errors": exc.errors(),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
if isinstance(exc, (HTTPException, StarletteHTTPException)):
|
|
|
|
|
|
|
|
log.error(
|
|
|
|
|
|
|
|
"HTTP exception occurred",
|
|
|
|
|
|
|
|
extra={
|
|
|
|
|
|
|
|
"error_category": "http",
|
|
|
|
|
|
|
|
"status_code": exc.status_code,
|
|
|
|
|
|
|
|
"detail": exc.detail if hasattr(exc, "detail") else str(exc),
|
|
|
|
|
|
|
|
"request": request_info,
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=exc.status_code,
|
|
|
|
status_code=exc.status_code,
|
|
|
|
content={
|
|
|
|
content={
|
|
|
|
"data": None,
|
|
|
|
"data": None,
|
|
|
|
"message": str(exc.detail),
|
|
|
|
"message": str(exc.detail) if hasattr(exc, "detail") else str(exc),
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
"status": ResponseStatus.ERROR,
|
|
|
|
"errors": [ErrorDetail(message=str(exc.detail)).model_dump()],
|
|
|
|
"errors": [
|
|
|
|
|
|
|
|
ErrorDetail(
|
|
|
|
|
|
|
|
message=str(exc.detail) if hasattr(exc, "detail") else str(exc)
|
|
|
|
|
|
|
|
).model_dump()
|
|
|
|
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(exc, SQLAlchemyError):
|
|
|
|
if isinstance(exc, SQLAlchemyError):
|
|
|
|
error_message, status_code = handle_sqlalchemy_error(exc)
|
|
|
|
error_message, status_code = handle_sqlalchemy_error(exc)
|
|
|
|
logging.error(
|
|
|
|
log.error(
|
|
|
|
f"Database Error | Error: {str(error_message)} | Request: {request_info}",
|
|
|
|
"Database error occurred",
|
|
|
|
extra={"error_category": "database"},
|
|
|
|
extra={
|
|
|
|
|
|
|
|
"error_category": "database",
|
|
|
|
|
|
|
|
"error_message": error_message,
|
|
|
|
|
|
|
|
"request": request_info,
|
|
|
|
|
|
|
|
"exception": str(exc),
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
return JSONResponse(
|
|
|
|
@ -139,9 +174,14 @@ def handle_exception(request: Request, exc: Exception):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Log unexpected errors
|
|
|
|
# Log unexpected errors
|
|
|
|
logging.error(
|
|
|
|
log.error(
|
|
|
|
f"Unexpected Error | Error: {str(exc)} | Request: {request_info}",
|
|
|
|
"Unexpected error occurred",
|
|
|
|
extra={"error_category": "unexpected"},
|
|
|
|
extra={
|
|
|
|
|
|
|
|
"error_category": "unexpected",
|
|
|
|
|
|
|
|
"error_message": str(exc),
|
|
|
|
|
|
|
|
"request": request_info,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
exc_info=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
return JSONResponse(
|
|
|
|
|