Skip to content
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

Merged

Conversation

emarteca
Copy link
Contributor

@emarteca emarteca commented Dec 6, 2024

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 the toString 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 are Throwable, 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.

@emarteca emarteca changed the title Adding support for Throwable error result types Adding support for Throwable error result types in Kotlin Dec 6, 2024
@emarteca emarteca marked this pull request as ready for review December 6, 2024 03:07
@emarteca emarteca mentioned this pull request Dec 6, 2024
@Manishearth
Copy link
Contributor

Manishearth commented Dec 6, 2024

Hm, so what i'd prefer is we add a #[diplomat::attr(auto, error)] that can be applied to types. Thoughts?

(and then the Kotlin backend errors when you use non error types in Result)

(we'd add something similar for JS)

@Manishearth
Copy link
Contributor

#446

@emarteca
Copy link
Contributor Author

emarteca commented Dec 6, 2024

Oh yeah that's a good idea -- then, we could only add Throwable on the types that are actually marked with the error attribute. I'll do that.

Also, it doesn't look like Kotlin enums support inheritance, so I'm thinking we just have the error attribute requirement for structs and opaques for now. What do you think about (in a subsequent PR) adding a Throwable wrapper for all the primitives, and Throwable wrappers for enums marked as error?

@Manishearth
Copy link
Contributor

Yeah, that would work!

@emarteca
Copy link
Contributor Author

emarteca commented Dec 7, 2024

Ok I added the error attribute! Good idea.

I'll do a followup PR to add the Throwable wrappers for primitives, and for enums marked with diplomat::attr(auto,error), if that sounds good?

Copy link
Contributor

@jcrist1 jcrist1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice!

@Manishearth Manishearth merged commit baa148b into rust-diplomat:main Dec 9, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants