"""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"}}, )