Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_middleware causes exceptions to not be raised/handled silently #5173

Closed
9 tasks done
fraser-langton opened this issue Jul 20, 2022 · 7 comments
Closed
9 tasks done
Labels
question Question or problem question-migrate

Comments

@fraser-langton
Copy link

First 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

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)

Description

When exceptions are raised on a subapp, the exceptions are not propagated all the way to see in console
In the example code
/info raises an exception and the full stacktrace is seen in console
/private/info does not raise the exception and only INFO: 127.0.0.1:56308 - "GET /info HTTP/1.1" 500 Internal Server Error is shown in console

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.74.0

Python Version

3.8.10

Additional Context

uvicorn==0.17.6

@fraser-langton fraser-langton added the question Question or problem label Jul 20, 2022
@fraser-langton
Copy link
Author

Upgrading to 0.79.0 the issue goes away, not sure if this was fixed intentionally (I couldn't find a closed issue) or inadvertently in which case might still be an issue elsewhere

@iudeen
Copy link
Contributor

iudeen commented Jul 21, 2022

What's the behavior if you add the Middleware to both of your apps? 🤔

@Kludex
Copy link
Member

Kludex commented Jul 21, 2022

Upgrading to 0.79.0 the issue goes away, not sure if this was fixed intentionally (I couldn't find a closed issue) or inadvertently in which case might still be an issue elsewhere

It was solved on Starlette.

@fraser-langton
Copy link
Author

It was solved on Starlette.
Thanks @Kludex - was very confused!

@fraser-langton
Copy link
Author

Issue is back see my versions below, should I open an issue on starlette?

fastapi==0.88.0
starlette==0.23.0
uvicorn==0.20.0

@fraser-langton fraser-langton reopened this Dec 6, 2022
@Kludex
Copy link
Member

Kludex commented Dec 6, 2022

You can open a discussion on starlette.

@fraser-langton
Copy link
Author

Opened discussion in starlette

@tiangolo tiangolo reopened this Feb 27, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6256 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants