Skip to content

Commit ae6457e

Browse files
committed
Cleanup
1 parent f006d6f commit ae6457e

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

sentry_sdk/utils.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ def exceptions_from_error(
776776
# type: (...) -> Tuple[int, List[Dict[str, Any]]]
777777
"""
778778
Converts the given exception information into the Sentry structured "exception" format.
779-
This will return a list of exceptions in the format of the Exception Interface documentation:
779+
This will return a list of exceptions (a flattened tree of exceptions) in the
780+
format of the Exception Interface documentation:
780781
https://develop.sentry.dev/sdk/data-model/event-payloads/exception/
781782
782783
This function can handle:
@@ -800,56 +801,49 @@ def exceptions_from_error(
800801
parent_id = exception_id
801802
exception_id += 1
802803

803-
# Note: __suppress_context__ is True if the exception is raised with the `from` keyword.
804+
causing_exception = None
805+
exception_source = None
806+
807+
# Add any causing exceptions, if present.
804808
should_suppress_context = hasattr(exc_value, "__suppress_context__") and exc_value.__suppress_context__ # type: ignore
809+
# Note: __suppress_context__ is True if the exception is raised with the `from` keyword.
805810
if should_suppress_context:
806811
# Explicitly chained exceptions (Like: raise NewException() from OriginalException())
807812
# The field `__cause__` is set to OriginalException
808-
exception_has_explicit_causing_exception = (
813+
has_explicit_causing_exception = (
809814
exc_value
810815
and hasattr(exc_value, "__cause__")
811816
and exc_value.__cause__ is not None
812817
)
813-
if exception_has_explicit_causing_exception:
818+
if has_explicit_causing_exception:
819+
exception_source = "__cause__"
814820
causing_exception = exc_value.__cause__ # type: ignore
815-
816-
(exception_id, child_exceptions) = exceptions_from_error(
817-
exc_type=type(causing_exception),
818-
exc_value=causing_exception,
819-
tb=getattr(causing_exception, "__traceback__", None),
820-
client_options=client_options,
821-
mechanism=mechanism,
822-
exception_id=exception_id,
823-
parent_id=parent_id,
824-
source="__cause__",
825-
full_stack=full_stack,
826-
)
827-
exceptions.extend(child_exceptions)
828-
829821
else:
830822
# Implicitly chained exceptions (when an exception occurs while handling another exception)
831823
# The field `__context__` is set in the exception that occurs while handling another exception,
832824
# to the other exception.
833-
exception_has_implicit_causing_exception = (
825+
has_implicit_causing_exception = (
834826
exc_value
835827
and hasattr(exc_value, "__context__")
836828
and exc_value.__context__ is not None
837829
)
838-
if exception_has_implicit_causing_exception:
830+
if has_implicit_causing_exception:
831+
exception_source = "__context__"
839832
causing_exception = exc_value.__context__ # type: ignore
840833

841-
(exception_id, child_exceptions) = exceptions_from_error(
842-
exc_type=type(causing_exception),
843-
exc_value=causing_exception,
844-
tb=getattr(causing_exception, "__traceback__", None),
845-
client_options=client_options,
846-
mechanism=mechanism,
847-
exception_id=exception_id,
848-
parent_id=parent_id,
849-
source="__context__",
850-
full_stack=full_stack,
851-
)
852-
exceptions.extend(child_exceptions)
834+
if causing_exception:
835+
(exception_id, child_exceptions) = exceptions_from_error(
836+
exc_type=type(causing_exception),
837+
exc_value=causing_exception,
838+
tb=getattr(causing_exception, "__traceback__", None),
839+
client_options=client_options,
840+
mechanism=mechanism,
841+
exception_id=exception_id,
842+
parent_id=parent_id,
843+
source=exception_source,
844+
full_stack=full_stack,
845+
)
846+
exceptions.extend(child_exceptions)
853847

854848
# Add child exceptions from an ExceptionGroup.
855849
is_exception_group = exc_value and hasattr(exc_value, "exceptions")

0 commit comments

Comments
 (0)