Skip to content
Discussion options

You must be logged in to vote

I haven't confirmed this works, but you should be able to do:

async def custom_request_validation_exception_handler(
    request: Request, exc: RequestValidationError
) -> JSONResponse:
    # change this as you like, including possibly the return type
    return JSONResponse(
        status_code=HTTP_422_UNPROCESSABLE_ENTITY, content={"detail": exc.errors()}
    )

...

app = FastAPI(...)

...

app.add_exception_handler(
    fastapi.exceptions.RequestValidationException,
    custom_request_validation_exception_handler
)

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #553 on February 28, 2023 11:58.