-
Notifications
You must be signed in to change notification settings - Fork 52
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
Adding support for Throwable
error result types in Kotlin
#741
Adding support for Throwable
error result types in Kotlin
#741
Conversation
…s a custom Kotlin exception type
Throwable
error result typesThrowable
error result types in Kotlin
Hm, so what i'd prefer is we add a (and then the Kotlin backend errors when you use non error types in Result) (we'd add something similar for JS) |
Oh yeah that's a good idea -- then, we could only add Also, it doesn't look like Kotlin enums support inheritance, so I'm thinking we just have the |
Yeah, that would work! |
Ok I added the I'll do a followup PR to add the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice!
More work on #697
Right now, if there is an error result passed from Rust to Kotlin, it is always thrown as a
RuntimeException
, which just calls thetoString
on the error. It is more useful to allow Kotlin to inspect the error. If the error result is represented by a Kotlin class, then we should be able to use the error result as an object of that class.This also allows more finegrained error handling, instead of just having every error result from diplomat be a
RuntimeException
.This PR adds support for returning structs and opaques as custom exceptions when they take the error position in a result. This is done by making all structs and opaques used in the error position of the result inherit from
Exception
-- then, they areThrowable
, and in the.err()
case on results the error result object itself can be thrown. Unfortunately this still doesn't handle the primitive error case.Note: this PR also adds a new attribute,
diplomat::attr(auto, error)
, that can be used to mark types. An error is thrown if non-type item is marked with this attribute. And, in the Kotlin backend, there is an error thrown if an opaque or struct not marked with this attribute is used in the error position in a result.