Spaces:
Running
Running
File size: 1,371 Bytes
2129c29 | 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | """Global exception handlers for NLProxy server.
Author: IntelliDeep Labs Team
License: BSL 1.1
"""
from __future__ import annotations
import logging
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
def register_exception_handlers(app: FastAPI) -> None:
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
request_id = getattr(request.state, "request_id", "unknown")
logger.warning(
"[%s] HTTP %s: %s",
request_id,
exc.status_code,
exc.detail,
)
return JSONResponse(
status_code=exc.status_code,
content={"error": {"type": "http_error", "message": exc.detail}},
)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception) -> JSONResponse:
request_id = getattr(request.state, "request_id", "unknown")
logger.exception(
"[%s] Unhandled exception: %s",
request_id,
exc,
)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"error": {"type": "internal_error", "message": "Internal server error"}},
)
|