question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation for FastAPI/Starlette ASGI not correct

See original GitHub issue

Environment

How do you use Sentry? Sentry SaaS (sentry.io)

Which SDK and version? python sentry-sdk==1.4.3 via pipenv

Steps to Reproduce

  • Followed the installation instructions in an FastAPI app
  • Run a HTTP request against the API

Expected Result

Sentry logging errors

Actual Result

AttributeError: 'SentryAsgiMiddleware' object has no attribute 'add_middleware'

Analysis

I think the issue can be fixed by changing the way the middleware is added:

app = FastAPI()
app.add_middleware(SentryAsgiMiddleware)

That being said, I am not getting errors logged when I call a route that runs raise HTTPException(status_code=500, detail="Test"). So I think the middleware is still not working.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
AbhiPrasadcommented, Oct 5, 2021

Thank you for raising an issue. You’ll need to use the ASGI middleware in conjugation with app.exception_handler. We have to improve our documentation here - and in general have a better story around FastAPI.

app = FastAPI()

# Uncaught exceptions (like `raise Exception`) should propagate correctly
# to Sentry's error handler
# Middleware will also enable Sentry performance monitoring to work as expected
app.add_middleware(SentryAsgiMiddleware)

# To catch raised `HTTPException` exceptions as per:
# https://fastapi.tiangolo.com/tutorial/handling-errors/
#
# You might have to add something similar for `RequestValidationError`
@app.exception_handler(HTTPException)
async def custom_http_exception_handler(request, e):
    with sentry_sdk.configure_scope() as scope:
        scope.set_context("request", request)
        sentry_sdk.capture_exception(e)

    return await http_exception_handler(request, e)

For the future, we probably should just monkey patch the exception_handler so we take care of this out of the box.

1reaction
lsmith77commented, Oct 5, 2021

so if create an exception that isn’t caught by FastAPI (f.e. raise ValueError("nope")) it seems to log it properly. So am I wrong to assume that HTTPException should be converted or does this indeed just show that the middleware isn’t properly installed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Middleware - FastAPI
As FastAPI is based on Starlette and implements the ASGI specification, you can use any ASGI middleware. A middleware doesn't have to be...
Read more >
Starlette
Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python. It is production-ready, and gives you the ......
Read more >
Middleware - Starlette
An abstract class that allows you to write ASGI middleware against a request/response interface. Usage. To implement a middleware class using BaseHTTPMiddleware ...
Read more >
Responses - Starlette
Responses. Starlette includes a few response classes that handle sending back the appropriate ASGI messages on the send channel.
Read more >
Static Files - Starlette
Defaults to True . You can combine this ASGI application with Starlette's routing to provide comprehensive static file serving.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found