[QUESTION] Handling Exception Response format
See original GitHub issueHello.
I want to change the validation error response and make it inside app
object.
I’ve found this example:
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)
But can’t understand how to add this function to app
object without decorator:
app = FastAPI(
title='Bla API',
description='Bla description',
APIRoute('/api', api.toggle, methods=['POST'],
description='Switch interface state',
response_description='Interface successfully switched',
response_class=JSONResponse,
response_model=api.Success,
responses={**api.post_responses},
),
...
Issue Analytics
- State:
- Created 4 years ago
- Comments:20 (6 by maintainers)
Top Results From Across the Web
Best Practices for REST API Error Handling - Baeldung
3.1. The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: 400 Bad...
Read more >Guide to Spring Boot REST API Error Handling - Toptal
Let's learn how to handle exceptions in Spring Boot properly and wrap them into a better JSON representation to make life easier for...
Read more >Handling Exceptions Returned from the Web API
You'll learn to how to return 500, 404, and 400 exceptions and how to handle them on your Web page. Basic Exception Handling...
Read more >Servlet Exception and Error Handling Example Tutorial
The problem with this response is that it's of no value to user. Also it's showing our application classes and server details to...
Read more >Complete Guide to Exception Handling in Spring Boot
Spring provides a very elegant solution to this problem in form of “controller advice”. Let's study them. @ControllerAdvice. Why is it called " ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@MacMacky Ok. Thanks again for your help.
may be you can try this:
And in main.py