Spaces:
Running
on
T4
Running
on
T4
File size: 765 Bytes
fa85955 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
"""
Exception handlers and error response shaping.
"""
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from app.api.v1.schemas import ErrorResponse
from app.core.logging import get_logger
logger = get_logger(__name__)
def init_exception_handlers(app: FastAPI) -> None:
@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception):
request_id = getattr(request.state, "request_id", None)
logger.exception(f"Unhandled error: {exc}")
payload = ErrorResponse(
detail="Internal server error",
code="INTERNAL_ERROR",
request_id=request_id,
).dict()
return JSONResponse(status_code=500, content=payload)
|