-
|
i want to change the status code and response body for "validation error",what should i do ? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
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
) |
Beta Was this translation helpful? Give feedback.
-
but i want this exception shows in doc,it looks like must change the source code |
Beta Was this translation helpful? Give feedback.
-
|
it looks like i cant cover the "RequestValidationError", so i create a new exception like this: async def custom_request_validation_exception_handler( app.add_exception_handler( and i manually raise this exception |
Beta Was this translation helpful? Give feedback.
-
|
Yep, as @dmontagu says. It's documented here: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions |
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
I haven't confirmed this works, but you should be able to do: