Unwrapping failures erases exception type information
See original GitHub issueLet’s say I unwrap a failure and want to catch it using try-except.
The only way to do so currently is:
from returns.result import Result
from returns.functions import safe
from returns.primitives.exceptions import UnwrapFailedError
@safe
def foo(i) -> Result[int, Exception]:
if i == 0:
raise ValueError("problem")
return i + 5
try:
result = foo('a') # Will raise TypeError
# do stuff
result.unwrap()
except UnwrapFailedError as e:
try:
raise e.halted_container.failure() from e
except ValueError:
print("Don't use zero")
except TypeError:
print("Dont use strings")
This makes exception handling code more complex than it should be.
Naturally you could have also used:
@safe
def handle_err(e) -> Result[None, Exception]:
if isinstance(e, ValueError):
print("Don't use zero")
elif isinstance(e, TypeError):
print("Dont use strings")
else:
raise e from e
try:
foo('a').rescue(handle_err).unwrap()
except UnwrapFailedError:
print("Unhandled exception!!!")
But the error handling code above simply feels unpythonic and awkward.
Rescuing from an error fits the usecase where you want to log the error and proceed or any other generic error handling situation.
You could make rescue()
accept a mapping between types to callables and call the appropriate error handler when an exception occured but I don’t see why you’d have to do so.
It seems to me that the UnwrapFailedError
exception is not necessary and hinders code readability.
The try-except clause was created for such situations. We should use it.
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
Error Handling - The Rust Programming Language
To “unwrap” something in Rust is to say, “Give me the result of the computation, and if there was an error, panic and...
Read more >If/guard-let-catch for conveniently accessing both Result type ...
Motivation A few weeks ago, I had asked on Twitter if developers prefer error handling in Swift by using throws vs. returning Result....
Read more >What does "Fatal error: Unexpectedly found nil while ...
Basically you tried to use a nil value in places where Swift allows only non-nil ones, by telling the compiler to trust you...
Read more >[Question / Discussion] Why is .unwrap() so heavily ... - Reddit
But the problem with that bug is that there is no way to communicate that error to the user code, because the function...
Read more >Using unwrap() in Rust is Okay - Andrew Gallant's Blog
Should unwrap() be used for error handling? What about “recoverable” vs “unrecoverable” errors? So one should never panic?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Now it is
result.alt(raise_exception).unwrap()
@thedrow I like
unwrap_or_raise()
!