-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
# main and sub-app
main_app = FastAPI()
sub_app = FastAPI()
# Add a view to sub-app that raises an error
@sub_app.get("/")
async def fail():
raise ValueError("oops")
# mount the sub app at /test
# note: doing this after the @app.middleware does not change the problem
main_app.mount("/test", sub_app)
# no-op middleware on main app
@main_app.middleware("http")
async def middleware(request, handler):
return await handler(request)
# Now run me like `uvicorn example:main_app --reload --port 8000`
# GET /test/
# Notice that the server just prints 500 Internal Server Error (what was the error?)
# Now comment out the middleware
# Notice that the server prints the stack trace now DescriptionWhen an unhandled exception occurs in a route handler, the server automatically responds with a 500 error response. It also prints the stack trace to the log, so the developer can tell what went wrong. However, if you mount a sub-application AND add a middleware to the root application, exceptions raised in the sub-application will NOT print the stack trace, you will only see a "500 Internal Server Error" logged. This behavior started in 0.71.0. Operating SystemLinux Operating System DetailsNo response FastAPI VersionAnything after 0.71.0 Python Version3.9.10 Additional ContextI feel like this has something to do with Starlette's error handling middleware? The answer might be "yeah don't set up your application that way" but if that's the case it would be helpful to warn about some middleware/subapplication quirks in the documentation. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Hi @waweber, faced the same issue and using the suggestion from @haomingyin on issue #1802 worked. |
Beta Was this translation helpful? Give feedback.
-
I can confirm this behaviour, having the same problem in my environment. |
Beta Was this translation helpful? Give feedback.
-
This issue still persists with latest versions of |
Beta Was this translation helpful? Give feedback.
-
This issue appears to be resolved with latest versions of |
Beta Was this translation helpful? Give feedback.
-
This issue is once again present with latest versions of |
Beta Was this translation helpful? Give feedback.
This issue appears to be resolved with latest versions of
uvicorn==0.24.0.post1
fastapi==0.104.1
starlette==0.27.0