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

chore: return expected errors without throwing exceptions #7

Open
ashneverdawn opened this issue Sep 12, 2024 · 0 comments
Open

chore: return expected errors without throwing exceptions #7

ashneverdawn opened this issue Sep 12, 2024 · 0 comments

Comments

@ashneverdawn
Copy link
Contributor

When a Result<> is returned from a function exposed to js via #[wasm_bindgen], the error is returned as an Exception.

Since panics also cause Exceptions, there is no way to tell if an Exception was caused by an expected error or a panic.

This matters because panics can potentially result in a corrupted wasm instance and should be handled by re-initializing a new wasm instance. Expected errors on the other hand (for example, the result of invalid user input) don't require re-initializing the entire wasm as they are just part of regular business logic.

For this reason, we would rather return expected errors as part of the Ok of this Result, instead of the Error.

So something like this:

#[wasm_bindgen]
pub fn my_func() -> Result<MyOkType, MyErrorType>

Would become something like this:

#[wasm_bindgen]
pub fn my_func() -> Result<Result<MyOkType, MyErrorType>, JsError>
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

No branches or pull requests

1 participant