aucourt-ingest/aucourt_ingest/api/errors.py

22 lines
709 B
Python
Raw Normal View History

"""Error handlers for the query API."""
from __future__ import annotations
import logging
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
def register_error_handlers(app: FastAPI) -> None:
@app.exception_handler(ValueError)
async def value_error_handler(request: Request, exc: ValueError):
return JSONResponse(status_code=400, content={"detail": str(exc)})
@app.exception_handler(Exception)
async def generic_error_handler(request: Request, exc: Exception):
logger.exception("Unhandled exception in query API")
return JSONResponse(status_code=500, content={"detail": "Internal server error"})