aucourt-ingest/aucourt_ingest/api/errors.py
slothitude 24cde4cdec Audit fixes: response_model validation, error handling, dead code, input sanitisation
- Add response_model to all 8 route endpoints for runtime validation and
  correct Swagger docs
- Remove global KeyError handler (routes catch it explicitly)
- Add catch-all Exception handler with logging for 500 responses
- Remove dead code in service.py get_case_graph (unused bucket variable)
- Explicit graph_backend validation in cmd_serve (memory|neo4j, else exit)
- Sanitise comma-separated query params (strip whitespace, filter empty)
- Move HTTPException to top-level import in routes.py
- Remove unused imports (Depends in dependencies.py, all_persona_names)
- Fix deprecated asyncio.get_event_loop() in test fixture

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-30 12:17:55 +10:00

21 lines
709 B
Python

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