Unable to catch `Exception` via exception_handlers since FastAPI 0.70.0
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
################################################
#
# Pay attention: below is Python 3.10 syntax
#
################################################
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from pydantic import ValidationError
async def internal_server_error(request: Request, exc: Exception | ValidationError) -> "JSONResponse":
return JSONResponse(content={"detail": "Internal Server Error"}, status_code=500)
app = FastAPI(
exception_handlers={
Exception: internal_server_error,
ValidationError: internal_server_error,
},
)
@app.get("/call_me")
async def raise_exception() -> None:
raise Exception("I am exception.")
Description
- Open the browser and call the endpoint:
/call_me
- It returns plain text:
Internal Server Error
- But I excepted it return JSON:
{"detail": "Internal Server Error"}
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
Python 3.10.0
Additional Context
It works fine on FastAPI 0.68.2. Issue is also affected 0.69.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:15 (7 by maintainers)
Top Results From Across the Web
Release Notes - FastAPI
After the dependency with yield handles the exception (or not) the exception is raised again. So that any exception handlers can catch it,...
Read more >Catch `Exception` globally in FastAPI - Stack Overflow
In case you want to capture all unhandled exceptions (internal server error), there's a very simple way of doing it. Documentation
Read more >fastapi - PyPI
Robust: Get production-ready code. With automatic interactive documentation. Standards-based: Based on (and fully compatible with) the open standards for APIs: ...
Read more >Implementing FastAPI Services - Camillo Visini
To handle custom exceptions occurring at the service layer, as instances of class AppExceptionCase , a respective exception handler is added to ...
Read more >fastapi Changelog - pyup.io
After the dependency with `yield` handles the exception (or not) the exception is raised again. So that any exception handlers can catch it,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
I’m eagerly looking forward to https://github.com/tiangolo/fastapi/pull/4145 getting merged. 😄
Fixed by https://github.com/encode/starlette/pull/1262.