Skip to content

Commit

Permalink
Merge pull request datalad#7647 from yarikoptic/enh-cause
Browse files Browse the repository at this point in the history
ENH: Add information about __cause__ exception in the "exception" printout within result record and its short string version
  • Loading branch information
yarikoptic authored Oct 14, 2024
2 parents 175e81a + e7763a5 commit f513885
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions datalad/support/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def format_short(self):
-------
str
"""
return self.name + '(' + self.message + ')'
s = self.name + '(' + self.message + ')'
if exc_cause := getattr(self.tb, '__cause__', None):
s += f' -caused by- {format_exception_with_cause(exc_cause)}'
return s

def format_with_cause(self):
"""Returns a representation of the original exception including the
Expand Down Expand Up @@ -153,13 +156,6 @@ def format_oneline_tb(exc, tb=None, limit=None, include_str=True):
# startup.
from datalad import cfg

if include_str:
# try exc message else exception type
leading = exc.message or exc.name
out = "{} ".format(leading)
else:
out = ""

if tb is None:
tb = traceback.TracebackException.from_exception(
exc,
Expand All @@ -168,6 +164,15 @@ def format_oneline_tb(exc, tb=None, limit=None, include_str=True):
capture_locals=False,
)

if include_str:
# try exc message else exception type
leading = exc.message or exc.name
out = "{} ".format(leading)
if exc_cause := getattr(tb, '__cause__', None):
out += f'-caused by- {format_exception_with_cause(exc_cause)} '
else:
out = ""

entries = []
entries.extend(tb.stack)
if tb.__cause__:
Expand Down
8 changes: 4 additions & 4 deletions datalad/support/tests/test_captured_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def f2():

estr_full = captured_exc.format_oneline_tb(10)

assert_re_in(r"new message \[test_captured_exception.py:test_CapturedException:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr_full)
assert_re_in(r"new message \[test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr3)
assert_re_in(r"new message \[test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr2)
assert_re_in(r"new message \[test_captured_exception.py:f2:[0-9]+\]", estr1)
assert_re_in(r"new message -caused by- my bad again \[test_captured_exception.py:test_CapturedException:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr_full)
assert_re_in(r"new message -caused by- my bad again \[test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr3)
assert_re_in(r"new message -caused by- my bad again \[test_captured_exception.py:f:[0-9]+,test_captured_exception.py:f2:[0-9]+\]", estr2)
assert_re_in(r"new message -caused by- my bad again \[test_captured_exception.py:f2:[0-9]+\]", estr1)
# default: no limit:
assert_equal(estr_, estr_full)

Expand Down

0 comments on commit f513885

Please sign in to comment.