mozilla / uniffi-rs

a multi-language bindings generator for rust
https://mozilla.github.io/uniffi-rs/
Mozilla Public License 2.0
2.48k stars 211 forks source link

More idiomatic error classes in Python #223

Open savi2w opened 3 years ago

savi2w commented 3 years ago

Complementing #215 Putting this here so it doesn't get lost

Having nested class for each error like this is pretty un-idiomatic in python. I'm not sure how to expose our errors in a better way though

_Originally posted by @rfk in https://github.com/mozilla/uniffi-rs/pull/215#discussion_r467660744_

┆Issue is synchronized with this Jira Task ┆Issue Number: UNIFFI-13

rfk commented 3 years ago

One thought here is, it would be nice to make the container class a superclass for the specific cases, so we could have something like:

try:
    a_function_that_throws():
except ArithmeticError.IntegerOverflow:
    do_something()
except ArithmeticError.DivideByZero:
    do_something_else()

But could also generically catch any ArithmeticError and then inspect it in more detail, like:

try:
    a_function_that_throws():
except ArithmeticError as e:
    do_something()
    if isinstance(e, ArithmeticError.IntegerOverflow):
        so_something_extra()

Idiomatic python would have these all in one flat namesace (e.g. ArithmeticError, IntegerOverflowError and DivideByZeroError classes all in the top level of the module) but being able to do the above might be a nice compromise.