-
|
Following the discussion in #14964, deprecating Currently, exception handlers must return a Two possible paths forward:
Allow exception handlers to return dicts or Pydantic models directly, similar to regular route handlers: class MyErrorBody(BaseModel):
message: str
@app.exception_handler(MyError, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
async def _(request: Request, exception: MyError) -> MyErrorBody:
return MyErrorBody(message=str(exception))This would enable Pydantic serialization for exception responses and feed the OpenAPI schema generation with error response types, which is currently not possible. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
For now, if you worry about the performance for errors / exception handlers, you could create a custom response with your serialization, it's 4 lines of code: https://fastapi.tiangolo.com/advanced/custom-response/#custom-response-class And you could also use something other than orjson, or a different format, etc. |
Beta Was this translation helpful? Give feedback.
For now, if you worry about the performance for errors / exception handlers, you could create a custom response with your serialization, it's 4 lines of code: https://fastapi.tiangolo.com/advanced/custom-response/#custom-response-class
And you could also use something other than orjson, or a different format, etc.