Skip to content

FastAPI's HTTPExceptions do not get sent with the error message as subtitle #2192

Closed

Description

@nicolassanmar

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.25.1

Steps to Reproduce

  1. Create 2 simple FastAPI routes that raise exceptions like this:
@router.get("/http-exception")
async def trigger_http_exception():
    raise HTTPException(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        detail="Intentional HTTPException, please ignore",
    )

class MyHTTPException(HTTPException):
    """This class should be used to throw our custom HTTPExceptions,
    as the base class does not provide the error message to Sentry"""

    def __init__(self, status_code: int, message: str):
        super().__init__(
            status_code=status_code,
            detail=message,
        )
        self.message = message  # required to make Sentry display subtitles on issues


@router.get("/http-exception-cause")
async def trigger_http_exception_with_cause():
    raise MyHTTPException(
        status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
        message="Intentional HTTPExceptionWithCause, please ignore",
    )
  1. Call the endpoint at /http-exception
  2. Call the endpoint at /http-exception-cause
  3. Go to Sentry's issue page, and confirm that only the MyHTTPException provides a subtitle for the issue with the error message, and the HTTPException does not.

I also contacted Sentry support and they also confirmed this is a bug.

Expected Result

Both exceptions should show the error message as the subtitle in the issue. On FastAPI HTTPExceptions this is called detail.

Actual Result

Only the custom MyHTTPException has the subtitle in Sentry issues.
image

I debugged printing the event on the before_send function and the event.exception.values[0].value as an empty string for HTTPExceptions:

image(1)

This is not the case with our custom exceptions, that as a workaround set the message field.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Jun 23, 2023
  2. added this to the milestone on Jun 23, 2023
  3. sentrivana commented on Jun 23, 2023

    @sentrivana
    Collaborator

    Hey @nicolassanmar, thanks for opening this, it'd definitely be nice to have the detail treated the same way we do messages. PRs are always welcome if you or anyone reading this feel like giving it a shot! We could either fix this in the Starlette integration, or more generally whenever we format the exception value and there's no message, but there is a detail.

  4. added a commit that references this issue on Jun 23, 2023
    d7e1e04
  5. goharShoukat commented on Jul 26, 2023

    @goharShoukat
    Contributor

    This is an excellent Issue raised! I have also encountered the same issue and am working around it with using Python's Exception class for now. But this really does need to be solved.

    raise HTTPException() from fastAPI does not send error report to sentry. however, invoking capture_error(HTTPException()) does. But then the details within the HTTPException do not get transmitted.

  6. sentrivana commented on Aug 2, 2023

    @sentrivana
    Collaborator

    Status update: there's an open pull request for this, but it needs some changes and the original contributor hasn't had time -- we'll take a look at it and finish it when time permits.

  7. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions