Skip to content

If Exception.__context__ is an ExceptionGroup, Sentry doesn't capture the ExceptionGroup's children #3913

@tmaxwell-anthropic

Description

@tmaxwell-anthropic

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.29.2

Steps to Reproduce

Here's a toy example that demonstrates the problem:

try:
    try:
        raise ExceptionGroup("group", [ValueError("child1"), ValueError("child2")])
    finally:
        raise TypeError("bar")
except BaseException:
    sentry_sdk.capture_exception()

sentry_sdk.capture_exception() receives the TypeError. TypeError.__context__ points to the ExceptionGroup; and then ExceptionGroup.exceptions points to [ValueError("child1"), ValueError("child2")].

Expected Result

The Sentry event should contain all four exceptions: the TypeError, the ExceptionGroup, and the sub-exceptions ValueError("child1") and ValueError("child2").

Additionally, the event should record that the ExceptionGroup is TypeError.__context__ as opposed to TypeError.__cause__.

Note that the sentry_sdk.utils.exceptions_from_error() function actually does this correctly, but it isn't getting invoked in this case. Here's a screenshot of how it looks if I patch the Sentry SDK to always use sentry_sdk.utils.exceptions_from_error():

Image

Actual Result

When the TypeError is passed into sentry_sdk.utils.exceptions_from_error_tuple(), it checks whether the TypeError is an ExceptionGroup or not: https://github.com/getsentry/sentry-python/blob/2.19.2/sentry_sdk/utils.py#L968-L1009 Because the TypeError is not an ExceptionGroup, it calls walk_exception_chain(), which examines TypeError.__context__ to find the ExceptionGroup; but it doesn't recurse into the sub-exceptions ValueError("child1") and ValueError("child2"). So the Sentry event only contains the TypeError and the ExceptionGroup:

Image

Also, the "Related Exceptions" tree in the above screenshot appears to be nonsensical.

I tested this with SDK version 1.29.2, but the bug appears to still be present in the latest version of the code.

Metadata

Metadata

Assignees

Labels

No fields configured for issues without a type.

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions