Provide a way to chain Err to Exception
See original GitHub issueHi,
Thanks for a cool library. I’ve tried to find a solid way to manage error cases in Python like Rust and found this elegant library.
We’re migrating some of our APIs using result and miss a feature to cover some common patterns we’ve found. As our library clients may not want to change their code base to be aware of result.Ok
or result.Err
(I hope so in the future though), we’ve decided to change our internal APIs only as a initial step.
So basically we have some of similar codes like
# Internal API
def _get_user() -> Result[User, str]:
...
def _raise(e):
raise e
# Public API
def get_user():
return _get_user().map_err(lambda e: _raise(UserNotFoundException)).unwrap()
This code works but have several ugliness including
- As lambda could not accept
raise
, we could not help to have a weird wrapper function like_raise
- We broke
map_err()
signature as the function accept a callable to return Err value
So basically, I’d like to ask having such API like below
def get_user():
# Return user when Ok otherwise raise UserNotFoundException which accepting error
return _get_user().ok_or(UserNotFoundException)
I don’t think ok_or()
is a right API name to reflect the feature (and is different from ok_or()
in Rust), so please understand the intention only. It seems opposite API of as_result()
as it translates Exception
to Result
and now I’m asking a API for Result.err
to Exception
.
I think introducing Try
API may be the right one to implement such feature but do not aware of the project’s road map, so any viable solution would be appreciated.
Thanks!
Issue Analytics
- State:
- Created a year ago
- Comments:10 (8 by maintainers)
@wbolster yes, i think so.
💡 just brainstorming…
i don’t quite like how this proposed api makes it almost inevitable to use a lambda construct for something that’s supposed to do something simple. (fwiw, i don’t like lambdas in general, in python at least.)
maybe this could handle
Type[BaseException]
in a special way? so that it can be either a callable (such as a lambda) or an exception class directly?however, nice errors, which this is about in the first place, typically require more than just an exception class, e.g. a message:
but sometimes it’s not just a string, so maybe also this?
… but these may be hard to type correctly.
passing an exception instance could work but would always construct it, even when it’s not needed: