Skip to content

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

@nicolassanmar

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions