-
First Check
Commit to Help
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
Operating SystemLinux Operating System DetailsNo response FastAPI Version0.70.0 Python VersionPython 3.10.0 Additional ContextIt works fine on FastAPI 0.68.2. |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments 2 replies
-
The Exception cannot be used for this purpose. You can inherit it tho |
Beta Was this translation helpful? Give feedback.
-
It works fine in 0.68.2 My use case: I`m trying to catch all unknown exceptions in code and responsing with JSON =) I think issue is relating to 0.69.0 (with new Starlette 0.15.0 and AnyIO) |
Beta Was this translation helpful? Give feedback.
-
You're right. I guess I put in my mind that is a "cannot" because Starlette will log the traceback if you use try to handle In any case, I cannot reproduce on Python 3.8, jfyk. |
Beta Was this translation helpful? Give feedback.
-
As a side note, I can't seem to catch the exceptions from a websocket endpoint in fastapi==0.68.2
The stack trace
App creation
|
Beta Was this translation helpful? Give feedback.
-
Same use case here, wanting to always return JSON even with generic exception. It seems to because of new starlette version encode/starlette#1175. |
Beta Was this translation helpful? Give feedback.
-
Same here. |
Beta Was this translation helpful? Give feedback.
-
Same here. Pinning starlette to the version 0.14.2 fixed the issue. |
Beta Was this translation helpful? Give feedback.
-
Fixed by encode/starlette#1262. |
Beta Was this translation helpful? Give feedback.
-
I'm eagerly looking forward to #4145 getting merged. 😄 |
Beta Was this translation helpful? Give feedback.
-
No... This is not fixed... |
Beta Was this translation helpful? Give feedback.
-
@Kludex did you validate against the just-released FastAPI 0.71.0 and confirm this is still an issue? |
Beta Was this translation helpful? Give feedback.
-
@phillipuniverse Using FastAPI 0.73.0 we're still facing such an issue |
Beta Was this translation helpful? Give feedback.
-
Using 0.78, I cannot reproduce. I see a JSON response in my browser using the code in OP. |
Beta Was this translation helpful? Give feedback.
-
We're facing this issue in tests, using the AsyncClient. Could you please try that? |
Beta Was this translation helpful? Give feedback.
-
Would you mind providing a minimal reproducible example? 🙏 |
Beta Was this translation helpful? Give feedback.
-
Facing a similar issue using FastAPI 0.80 and Starlette 0.19.1. The FastAPI app handles the Exception and returns JSON, but logs a stack trace with the following code:
The stack trace is identical to the above (only containing my unhandled error instead of Interestingly, if I convert the exception handler to middleware, there's no logged stack trace. I'd rather use the error handler, though.
Problem: an ASGI error gets logged with a stack trace when I try to handle Exception with an exception handler. |
Beta Was this translation helpful? Give feedback.
-
Still an issue in 0.109.0 Example
The route returns 500 as expected, but the exception is still printed with "Exception in ASGI application" |
Beta Was this translation helpful? Give feedback.
-
Still an issue in "0.111.0" |
Beta Was this translation helpful? Give feedback.
Would you mind providing a minimal reproducible example? 🙏