question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Custom response format for validation error

See original GitHub issue

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.ValidationError but 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 ValidationError on my own? I tried to search through issues and docs but couldn’t find anything related.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Olegt0rrcommented, Dec 14, 2020

This is supported now in the latest versions. 🎉

You can now add handlers for RequestValidationError and get the full Pydantic ValidationError: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions

Make sure to update your FastAPI version. The current latest is 0.27.0.

I’ll close this issue now, but feel free to add more comments or create new issues.

Docs builders don’t know about exception overriding. Please add an example this page.

Screenshot

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    return PlainTextResponse(str(exc), status_code=422)

image

10reactions
tiangolocommented, May 30, 2019

This is supported now in the latest versions. 🎉

You can now add handlers for RequestValidationError and get the full Pydantic ValidationError: https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions

Make sure to update your FastAPI version. The current latest is 0.27.0.

I’ll close this issue now, but feel free to add more comments or create new issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Validation Error customise format of the Response
My problem is how to format the response for the form requests, especially when this is not global but done on specific form...
Read more >
Changing the error message for "forced responses ...
As they mentioned, you have to go to your Library and create the message(s) first, and then go to Survey Options and tell...
Read more >
Set rules for your form - Google Docs Editors Help
Open a form in Google Forms. Add one of the following types of questions: Short answer; Paragraph; Checkboxes. Click More More . Click...
Read more >
How to make a custom FormRequest error response in ...
Hello,. I am using FormRequests to validate data, but want to return custom JSON on error. So far I was extending the FormRequest...
Read more >
JWT Validation - Custom response message - Microsoft Q&A
But I am looking for information on the error attributes from the policy error message to set the response. For example: The APIM...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found