middleware causes exceptions to not be raised/handled silently #1976
-
From issue in FastAPI - issue was said to be from starlette Was initially noticed in FastAPI 0.74.0, was fixed after 0.79.0 but has since regressed (FastAPI 0.88.0 it isn't working) When exceptions are raised on a subapp, the exceptions are not propagated all the way to see in console import uvicorn
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware
app = FastAPI()
@app.get("/info")
def info():
# raises Exception as expected, the traceback is seen in console
raise Exception
private_api = FastAPI()
@private_api.get("/info")
def info():
# exception is handled silently, no traceback is seen in console
raise Exception
app.mount("/private", private_api)
class Middleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
return await call_next(request)
app.add_middleware(Middleware) # when this is removed, the exceptions are raised for all routes
if __name__ == '__main__':
uvicorn.run(app, port=8000) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Created an issue for this: #1977. |
Beta Was this translation helpful? Give feedback.
-
Hello! I was tracking down why we couldn't bump our FastAPI version and found this discussion and fix. Exactly our issue! I was thrilled. The trouble is that the fix got un-fixed here... I did my best to look through the comments and it seems like the consensus is that Middleware handling is a headache at the best of times and subapps make it even harder. Not sure what the intended behavior is but wanted to see if we could re-open this discussion. Thanks for your time! |
Beta Was this translation helpful? Give feedback.
Created an issue for this: #1977.