diff --git a/app/init_api.py b/app/init_api.py index fbd11ee..c434d0c 100644 --- a/app/init_api.py +++ b/app/init_api.py @@ -6,14 +6,16 @@ from databases import Database from fastapi import FastAPI from fastapi import Request +from fastapi.exceptions import RequestValidationError from fastapi import Response from starlette.middleware.base import RequestResponseEndpoint - +from fastapi.exception_handlers import request_validation_exception_handler from app import logger from app import settings from app import state from app.adapters import mysql from app.api import api_router +from fastapi.responses import JSONResponse @asynccontextmanager @@ -53,6 +55,17 @@ async def http_middleware( logging.exception("Exception in ASGI application") return Response(status_code=500) + @app.exception_handler(RequestValidationError) + async def validation_exception_handler( + request: Request, exc: RequestValidationError + ) -> JSONResponse: + logging.warning( + "Request validation failed", + extra={"body": exc.body, "path": request.url.path}, + exc_info=exc, + ) + return await request_validation_exception_handler(request, exc) + return app