Skip to content

Commit f97d0aa

Browse files
committed
Improve FoldError formatting to show original exception messages
Fixes #65 Thanks to @RonnyPfannschmidt for the report!
1 parent 1468252 commit f97d0aa

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

effect/fold.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def __init__(self, accumulator, wrapped_exception):
1717
self.wrapped_exception = wrapped_exception
1818

1919
def __str__(self):
20-
tb_lines = traceback.format_tb(self.wrapped_exception[2])
20+
tb_lines = traceback.format_exception(*self.wrapped_exception)
2121
tb = ''.join(tb_lines)
22-
return "FoldError(%r, %r) -> ORIGINAL TRACEBACK FOLLOWS\n%s" % (
23-
self.accumulator, self.wrapped_exception, tb)
22+
st = (
23+
"<FoldError after accumulating %r> Original traceback follows:\n%s"
24+
% (self.accumulator, tb))
25+
return st.rstrip('\n')
2426

2527

2628
def fold_effect(f, initial, effects):

effect/test_fold.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ def test_fold_effect_errors():
6363
assert excinfo.value.wrapped_exception[0] is ZeroDivisionError
6464
assert str(excinfo.value.wrapped_exception[1]) == 'foo'
6565

66+
def test_fold_effect_str():
67+
effs = [Effect('a'), Effect(Error(ZeroDivisionError('foo'))), Effect('c')]
68+
69+
dispatcher = SequenceDispatcher([
70+
('a', lambda i: 'Ei'),
71+
])
72+
73+
eff = fold_effect(operator.add, 'Nil', effs)
74+
75+
with dispatcher.consume():
76+
with raises(FoldError) as excinfo:
77+
sync_perform(_base_and(dispatcher), eff)
78+
assert str(excinfo.value).startswith(
79+
"<FoldError after accumulating 'NilEi'> Original traceback follows:\n")
80+
assert str(excinfo.value).endswith('ZeroDivisionError: foo')
81+
6682

6783
def test_sequence():
6884
"""Collects each Effectful result into a list."""

0 commit comments

Comments
 (0)