-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a way to chain Err to Exception #93
Comments
something like but raising plain exceptions without any info (e.g. no message) are typically not nice APIs for consumers, so we should do better than that. i think this would need variants to a) simply raise an exception without even looking at the Err value, and b) a way to convert the Err value into a custom exception instance, maybe with a callable (could be a lambda) or something like that |
Yes, "unwrap" family seems to be a right one. I agree plain Exceptions may have low value (and we actually have added more information into the Exception although I skipped in the code above), and |
Rather than API itself, #95 has a issue which mypy could not detect signature due to the same issue (python/mypy#230). Any suggestions to provide better API? |
Hi guys, sorry been busy so haven't been involved. @ileixe have you look at #17, this was mentioned before and seems to be linked already to python/mypy#230 |
Could you elaborate on what you don't like about the suggested API? |
💡 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 res.unwrap_or_raise(ValueError) # raise ValueError() however, nice errors, which this is about in the first place, typically require more than just an exception class, e.g. a message: res.unwrap_or_raise(ValueError, "foo") # raise ValueError("foo") but sometimes it's not just a string, so maybe also this? res.unwrap_or_raise(MyException, *args, **kwargs) # raise MyException(*args, **kwargs) … 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: res.unwrap_or_raise(ValueError("foo")) # hmmm :/ |
I agree.
I'm inclined to provide a single specific way. From my perspective, all I want to pass some information from How about this? def unwrap_or_raise(self, exception: BaseException) -> NoReturn:
raise exception(self._value)
res = Err("My error message")
res.unwrap_or_raise(ValueError)
# This will be rendered as
# ValueError: my error message To be more specific, I can class CustomError:
def __repr__(self):
return "My error with information"
res = Err(CustomError())
res.unwrap_or_raise(ValueError)
# This will be rendered as
# ValueError: My error with information |
the basic exception constructor signature (which most built-in exceptions inherit) allows any number of arguments, which are later accessible via the
https://github.com/python/typeshed/blob/master/stdlib/builtins.pyi#L1771 most uses actually use only 1 argument, so perhaps |
@wbolster yes, i think so. |
Hi,
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
orresult.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
This code works but have several ugliness including
raise
, we could not help to have a weird wrapper function like_raise
map_err()
signature as the function accept a callable to return Err valueSo basically, I'd like to ask having such API like below
I don't think
ok_or()
is a right API name to reflect the feature (and is different fromok_or()
in Rust), so please understand the intention only. It seems opposite API ofas_result()
as it translatesException
toResult
and now I'm asking a API forResult.err
toException
.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!
The text was updated successfully, but these errors were encountered: