Skip to content

Commit

Permalink
doc about exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Jan 14, 2023
1 parent 3efcbba commit b71004c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Arbitrary Rust types (opaque)](feature/lang_rust_opaque.md)
- [Arbitrary Dart types (opaque)](feature/lang_dart_opaque.md)
- [Type alias](feature/lang_type_alias.md)
- [Exceptions](feature/lang_exceptions.md)
- [Zero copy](feature/zero_copy.md)
- [Stream / Iterator](feature/stream.md)
- [Async in Dart](feature/async_dart.md)
Expand Down
15 changes: 15 additions & 0 deletions book/src/feature/lang_exceptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Exceptions

1. For Result/Error, the`anyhow::Result`/`anyhow::Error` is supported. It will be automatically converted to a Dart Exception.
2. For `panic`s, it will also be automatically captured and converted to Dart exceptions.
3. [#582](https://github.com/fzyzcjy/flutter_rust_bridge/pull/582) adds support for error hierarchy, or arbitrary error types. For example, you can create your own `CustomError` (such as using `thiserror`), and it will automatically be converted to a new Dart class. The PR is already almost completed (with the hard work done), and the missing piece is fixing the test errors. I have provided some guidance about how to fix it at the PR comments as well.

## Example

For example, the following code, when called by Dart code, will throw Dart exceptions.

```rust,noplayground
pub fn f() -> anyhow::Result<i32> { bail!("oops I failed") }
pub fn g() -> i32 { panic!("oops I failed") }
```
2 changes: 1 addition & 1 deletion book/src/feature/lang_return_types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Return Types

The return type can be either `anyhow::Result<YourType>`, or `YourType` directly.
The return type can be either `anyhow::Result<YourType>`, or `YourType` directly. For exceptions (errors), please refer to [exceptions section](lang_exceptions.md) as well.

## Example

Expand Down

0 comments on commit b71004c

Please sign in to comment.