Skip to content

Commit 6c48b06

Browse files
Omer Katzsobolevn
authored andcommitted
Include original traceback when unwrapping an error (#57)
* Include original traceback when unwrapping an error. * Only include traceback if failure is an exception. * Address code review. * Added a test. * Happify lint.
1 parent cec0a00 commit 6c48b06

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

returns/result.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def value_or(self, default_value):
5959

6060
def unwrap(self):
6161
"""Raises an exception, since it does not have a value inside."""
62+
if isinstance(self._inner_value, Exception):
63+
raise UnwrapFailedError(self) from self._inner_value
64+
6265
raise UnwrapFailedError(self)
6366

6467
def failure(self):

tests/test_result/test_result_unwrap.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ def test_unwrap_failure():
1515
"""Ensures that unwrap works for Failure monad."""
1616
with pytest.raises(UnwrapFailedError):
1717
assert Failure(5).unwrap()
18+
19+
20+
def test_unwrap_failure_with_exception():
21+
"""Ensures that unwrap raises from the original exception."""
22+
expected_exception = ValueError('error')
23+
with pytest.raises(UnwrapFailedError) as excinfo:
24+
Failure(expected_exception).unwrap()
25+
26+
assert 'ValueError: error' in str(excinfo.getrepr())

0 commit comments

Comments
 (0)