Skip to content

feat(django): Add failed_request_status_codes - #7140

Open
mgaligniana wants to merge 1 commit into
getsentry:masterfrom
mgaligniana:GH-3134-add-failed-request-status-code-for-django
Open

feat(django): Add failed_request_status_codes#7140
mgaligniana wants to merge 1 commit into
getsentry:masterfrom
mgaligniana:GH-3134-add-failed-request-status-code-for-django

Conversation

@mgaligniana

@mgaligniana mgaligniana commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Description

Hi! In this PR I've added failed_request_status_code to the Django integration

  • I used AI and reviewed the changes.

Since English isn't my first language, I tried to keep the comments as simple and clear as possible, so they're easy to understand even for beginners like me who don't know the full Sentry product. Feel free to make any changes or suggestions!

Issues

@mgaligniana
mgaligniana requested a review from a team as a code owner August 10, 2026 00:27

@ericapisani ericapisani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR @mgaligniana , we really appreciate you taking the time to do so!

Overall the changes are looking great. However, I don't think we can exclude the 5xx codes from the failed_request_status_codes (left a more detailed comment below) and we'll need to address that before we can merge.

Don't hesitate to reach out with any questions!



def _capture_exception(
exc_info: "Any",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can tighten this typing a bit by copying what we do within the event_from_exception method that's being invoked within this function:

Suggested change
exc_info: "Any",
exc_info: "Union[BaseException, ExcInfo]",

This requires updating the import from sentry_sdk._types at the top to import ExcInfo:

    from sentry_sdk._types import (
        Event,
        EventProcessor,
        ExcInfo,
        Hint,
        NotImplementedType,
    )

Comment on lines +1826 to +1851
@pytest.mark.parametrize(
"integration_kwargs",
(
{},
{"failed_request_status_codes": set()},
{"failed_request_status_codes": {404}},
),
)
def test_failed_request_status_codes_unhandled_exception(
sentry_init, client, capture_events, integration_kwargs
):
"""
Exceptions Django gives up on are always reported, exactly once, no matter how
failed_request_status_codes is set.
"""
sentry_init(integrations=[DjangoIntegration(**integration_kwargs)])
events = capture_events()

_, status, _ = unpack_werkzeug_response(client.get(reverse("view_exc")))
assert status.lower() == "500 internal server error"

(event,) = events
(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
assert exception["mechanism"]["type"] == "django"
assert exception["mechanism"]["handled"] is False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I don't imagine there are many cases where someone would want to do this, if a user provides an empty set for failed_request_status_codes, or a non-empty set that doesn't include 5xx response codes, they will expect that no events are sent for these exceptions.

This will mean we need some additional changes to _got_request_exception to account for this, using a similar check to what you've added in _patch_response_for_exception.

Once that's done, we can look to remove this test case in favour of another parameterized case to the test you've added above (test_failed_request_status_codes), ideally one that confirms the behaviour by making a request against the view_exc endpoint to trigger a 500 error and ensure that no events are created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants