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.

any appetite for `.unwrap()` that prints the err value?

See original GitHub issue

Occasionally I have critical errors that cannot be remediated so i’d like to .unwrap(), but I have already captured valuable information in the Result’s Error type that get ignored in the UnwrapError.

Rather than checking for an error, then raising a new error with the captured message I created a .unwrap_print() function (not a great name) to relay this information:

from result import Result, Ok, Err, UnwrapError
from typing import NoReturn

 def unwrap_print(self) -> NoReturn:
    """
    Raises an `UnwrapError` but prints the error value.
    """
    raise UnwrapError(
        self,
        f"Called `Result.unwrap()` on an `Err` value:: {self.value.__repr__()}",
    )

Err.unwrap_print = unwrap_print  # type: ignore
>>> f.unwrap_print()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in unwrap_print
result.result.UnwrapError: Called `Result.unwrap()` on an `Err` value:: ValueError('testing')

Is this something that we can either add to the existing .unwrap() method or possibly add as a new method? I’m willing to make the PR but just wanted to see if there were any design constraints you’ve already considered first.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
noxasaxoncommented, Sep 2, 2022

it looks like the same behavior happens with .expect("msg") as well

0reactions
noxasaxoncommented, Sep 3, 2022

yeah i can make a PR later today!

Read more comments on GitHub >

github_iconTop Results From Across the Web

unwrap()` on an `Err` value: ()', libcore/result.rs:945 ... - GitHub
panicked at 'called `Result::unwrap()` on an `Err` value: ()', libcore/result.rs:945 This seems an old issue, and I saw the fix.
Read more >
What is unwrap in Rust, and what is it used for? - Stack Overflow
unwrap() is used here to handle the errors quickly. It can be used on any function that returns Result or Option (Option is...
Read more >
Go: the Good, the Bad and the Ugly - Sylvain Wallez
Unwrap() to get the wrapped error. We have to use the separate function errors.Unwrap(err) which uses dynamic typing tests to check if Unwrap ......
Read more >
Unwrapping Rust's errors. The Rust programming ... - Medium
unwrap() method can be used on any function that returns a Result<> , and just internally calls a panic, with no error message....
Read more >
Spock Framework Reference Documentation
def setupSpec() {} // runs once - before the first feature method def ... As you can see, Spock captures all values produced...
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