KevsOno's picture
Create exceptions.py
ea03631 verified
Raw History Blame Contribute Delete
703 Bytes
# core/exceptions.py – Global exception handler
import logging
import traceback
from fastapi import Request, FastAPI
from fastapi.responses import JSONResponse
from core.config import SENTRY_DSN
logger = logging.getLogger(__name__)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled: {exc}\n{traceback.format_exc()}")
if SENTRY_DSN:
import sentry_sdk
sentry_sdk.capture_exception(exc)
return JSONResponse(
status_code=500,
content={"detail": "Internal engine error.", "code": "INTERNAL_ERROR"},
)
def add_exception_handlers(app: FastAPI):
app.add_exception_handler(Exception, global_exception_handler)