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

[experimental-feature] Return custom Rust errors #2369

Closed
oo-work opened this issue Oct 24, 2024 · 8 comments
Closed

[experimental-feature] Return custom Rust errors #2369

oo-work opened this issue Oct 24, 2024 · 8 comments

Comments

@oo-work
Copy link

oo-work commented Oct 24, 2024

Now there is an Error in a third-party crate, which may be like this.

#[derive(Debug)]
pub enum CustomError {
   ConnectionError(Box<dyn std::error::Error + Send + Sync>),
   Io(std::io::Error),
   Other(Box<dyn std::error::Error + Send + Sync>),
   EOF,
}

flutter_rust_bridge.yaml

rust_input: crate::api,crate-name::error

When returning a Result<(), CustomError), Dart uses print(e.runtimeType); to print out CustomErrorImpl.

pub fn foo() -> Result<(), CustomError>{
    ...
}
try {
    await foo();
} catch(e) {
    print(e.runtimeType);
}

I want it to be a CustomError and have access to the type in the enum and call toString().

But dart generates such code, and the Exception type is not CustomError.

abstract class CustomError implements RustOpaqueInterface {
}
Copy link

welcome bot commented Oct 24, 2024

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 24, 2024

Here your CustomError should auto implements FrbException but indeed it does not.

Since you are using the experimental feature of auto scanning a whole third party crate, I guess this may be the cause.

A workaround may be, could you please try to have your CustomError in your main crate (instead of third party crate), or use the manual way of mirroring the 3rd party crate?

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Oct 24, 2024
@fzyzcjy fzyzcjy changed the title Return custom Rust errors [experimental-feature] Return custom Rust errors Oct 24, 2024
@oo-work
Copy link
Author

oo-work commented Oct 31, 2024

After I use mirror, Rust struct can be translated into FrbException. But Rust enum cannot be used as FrbException, it will always be translated into RustOpaqueInterface

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 31, 2024

https://cjycode.com/flutter_rust_bridge/guides/third-party/manual/external-types#example Seems that mirror is:

Enums with struct variants are not yet supported

A workaround may be, create your own enum in first party package (and convert it between the 3rd party one).

@oo-work
Copy link
Author

oo-work commented Nov 1, 2024

I tried changing the enum to a normal struct and it working.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Nov 1, 2024

Happy to see that!

@fzyzcjy
Copy link
Owner

fzyzcjy commented Nov 5, 2024

Close since this seems to be solved, but feel free to reopen if you have any questions!

@fzyzcjy fzyzcjy closed this as completed Nov 5, 2024
@fzyzcjy fzyzcjy removed the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Nov 5, 2024
Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants