Replies: 8 comments
-
|
You could add a custom handler for 422 errors: https://fastapi.tiangolo.com/tutorial/handling-errors/#installing-custom-handlers And extract the original message from the Let me know if that works for you. Otherwise, we can think about refactoring how |
Beta Was this translation helpful? Give feedback.
-
|
Some way of providing custom handler for validation errors would be nice. Currently I added handler for 422 errors but it doesn’t solve the issue entirely. I could reformat the dict, however it would make more sense to just get the original exception somehow and prices that. The other thing is, adding handler for 422 status doesn’t overwrite APIdocs for that status even if I added custom 422 definition in |
Beta Was this translation helpful? Give feedback.
-
|
First, about having custom validation handlers, I'll take it as a feature request. For overriding 422 errors, I would need to check it. I think that might be another feature request/issue too. |
Beta Was this translation helpful? Give feedback.
-
|
This is supported now in the latest versions. 🎉 You can now add handlers for Make sure to update your FastAPI version. The current latest is I'll close this issue now, but feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
Docs builders don't know about exception overriding. Screenshot@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=422) |
Beta Was this translation helpful? Give feedback.
-
|
This needs to be reopened. There's no sense in customizing an error response message if it can't be picked up by the API docs to inform users. |
Beta Was this translation helpful? Give feedback.
-
|
Is there a way to hide 422 error code from documentation? |
Beta Was this translation helpful? Give feedback.
-
For future reference: |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
How can I reformat default response for validation errors? I find the default formatting too verbose for my app, and wanted to return errors in different structure.
I thought of adding error handler explicitly for
pydantic.ValidationErrorbut this is ignored as the error is caught before that and handled internally by FastAPI:https://github.com/tiangolo/fastapi/blob/4a1648b04e74b5943f3ffadf92737f7d9c8201af/fastapi/routing.py#L68-L72
Is this possible to override default formatter and handle
ValidationErroron my own? I tried to search through issues and docs but couldn't find anything related.Beta Was this translation helpful? Give feedback.
All reactions