Skip to content

Commit

Permalink
Log validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Aug 8, 2024
1 parent f5139d4 commit a62f04d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/init_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit a62f04d

Please sign in to comment.