mozilla / uniffi-rs

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

Kotlin errors being `FooErrorException` is awkward. #442

Closed mhammond closed 3 years ago

mhammond commented 3 years ago

eg, ArithmeticErrorException here - all other bindings have the more natural ArithmeticError as defined in the udl.

It's grating on me in https://github.com/mozilla/application-services/pull/4068 so thought I'd ask if this really is idiomatic?

┆Issue is synchronized with this Jira Task

rfk commented 3 years ago

I agree this is pretty yuck :-/

Naively, it feels like the right thing to do would be to have the exception class named ArithmeticError just like in other bindings. If calling it "FooException" is really important for idiomatic Kotlin reasons, we could also consider translating error classes named FooError into FooException automatically in the bindings.

@grigoryk maybe you can give us a bit of a gut-check here from the PoV of a consumer? Looking at the FxA error class wrappers here, would it be appropriate for us to rename fxaclient.FxaErrorException to just fxaclient.FxaError?

npars commented 3 years ago

In java a "FooError" would typically be expected to extend Error and would be considered fatal. Meanwhile "FooException" would be expected to extend Exception and be non-fatal / catchable. I assume that was the original intention behind tacking on "Exception" at the end.

With that in mind, I would think translating Error -> Exception would be the cleanest solution.

jhugman commented 3 years ago

@npars is correct: from a JVM point of view, Error means something very specific. Removing an Error suffix before appending Exception may be the write thing to do here, even if low priority.

Stymergic hint: the quickest dirtiest path I can think of would be doing this in class_name_kt in gen_kotlin.rs.

grigoryk commented 3 years ago

ditto JVM Error meaning.

I agree with @jhugman's suggestion RE dropping Error before the name conversion, so we end up w/ ArithmeticException, not ArithmeticErrorException.

rfk commented 3 years ago

I support the direction we're going here, and it seems inline with other name-munging that we do in order to make the generated bindings more idiomtic. To sum up, I believe the consensus desired behavior to be:

I think this will actually be a pretty self-contained change. The mechanics of our Kotlin error handling are already split into two pieces for any given FooError:

So concretely, I think we can have the change we want here by adjusting only the name-generating logic in the exception wrapper class:

I do want to flag though, that this will potentially interact with the work proposed in #460. In fact, it could be a really nice way for someone like @bendk to ease in to the logic for handling errors before taking on that bigger piece of work 😁

sebastient commented 9 months ago

It would be helpful for the documentation on defining error types in UDL clarifies that errors cannot be named Error which is typical in Rust but fails because this munges the name to Exception, which it cannot be.

mhammond commented 9 months ago

Kotlin renaming Error to Exception is expected and should not cause problems. Can you please open a new issue describing your problem in more detail?