question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unwrapping failures erases exception type information

See original GitHub issue

Let’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:closed
  • Created 4 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
sobolevncommented, Sep 8, 2021

Now it is result.alt(raise_exception).unwrap()

1reaction
sobolevncommented, Apr 1, 2019

@thedrow I like unwrap_or_raise()!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found