@@ -801,7 +801,8 @@ def exceptions_from_error(
801801 # type: (...) -> Tuple[int, List[Dict[str, Any]]]
802802 """
803803 Converts the given exception information into the Sentry structured "exception" format.
804- This will return a list of exceptions in the format of the Exception Interface documentation:
804+ This will return a list of exceptions (a flattened tree of exceptions) in the
805+ format of the Exception Interface documentation:
805806 https://develop.sentry.dev/sdk/data-model/event-payloads/exception/
806807
807808 This function can handle:
@@ -825,56 +826,49 @@ def exceptions_from_error(
825826 parent_id = exception_id
826827 exception_id += 1
827828
828- # Note: __suppress_context__ is True if the exception is raised with the `from` keyword.
829+ causing_exception = None
830+ exception_source = None
831+
832+ # Add any causing exceptions, if present.
829833 should_suppress_context = hasattr (exc_value , "__suppress_context__" ) and exc_value .__suppress_context__ # type: ignore
834+ # Note: __suppress_context__ is True if the exception is raised with the `from` keyword.
830835 if should_suppress_context :
831836 # Explicitly chained exceptions (Like: raise NewException() from OriginalException())
832837 # The field `__cause__` is set to OriginalException
833- exception_has_explicit_causing_exception = (
838+ has_explicit_causing_exception = (
834839 exc_value
835840 and hasattr (exc_value , "__cause__" )
836841 and exc_value .__cause__ is not None
837842 )
838- if exception_has_explicit_causing_exception :
843+ if has_explicit_causing_exception :
844+ exception_source = "__cause__"
839845 causing_exception = exc_value .__cause__ # type: ignore
840-
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 = "__cause__" ,
850- full_stack = full_stack ,
851- )
852- exceptions .extend (child_exceptions )
853-
854846 else :
855847 # Implicitly chained exceptions (when an exception occurs while handling another exception)
856848 # The field `__context__` is set in the exception that occurs while handling another exception,
857849 # to the other exception.
858- exception_has_implicit_causing_exception = (
850+ has_implicit_causing_exception = (
859851 exc_value
860852 and hasattr (exc_value , "__context__" )
861853 and exc_value .__context__ is not None
862854 )
863- if exception_has_implicit_causing_exception :
855+ if has_implicit_causing_exception :
856+ exception_source = "__context__"
864857 causing_exception = exc_value .__context__ # type: ignore
865858
866- (exception_id , child_exceptions ) = exceptions_from_error (
867- exc_type = type (causing_exception ),
868- exc_value = causing_exception ,
869- tb = getattr (causing_exception , "__traceback__" , None ),
870- client_options = client_options ,
871- mechanism = mechanism ,
872- exception_id = exception_id ,
873- parent_id = parent_id ,
874- source = "__context__" ,
875- full_stack = full_stack ,
876- )
877- exceptions .extend (child_exceptions )
859+ if causing_exception :
860+ (exception_id , child_exceptions ) = exceptions_from_error (
861+ exc_type = type (causing_exception ),
862+ exc_value = causing_exception ,
863+ tb = getattr (causing_exception , "__traceback__" , None ),
864+ client_options = client_options ,
865+ mechanism = mechanism ,
866+ exception_id = exception_id ,
867+ parent_id = parent_id ,
868+ source = exception_source ,
869+ full_stack = full_stack ,
870+ )
871+ exceptions .extend (child_exceptions )
878872
879873 # Add child exceptions from an ExceptionGroup.
880874 is_exception_group = exc_value and hasattr (exc_value , "exceptions" )
0 commit comments