derive(JsError)
Allows returning an error from a wasm_bindgen
function without a separate API.
Motivation
wasm_bindgen
requires a function returning Result
to either be Result<T, JsError>
or Result<T, E> where E: Into<JsValue>
.
If you wish to return any other error type into Javascript,
the recommendation is to have an internal function that returns a normal Result<T, E>
,
and to have a public API that wraps the function's return value into a Result<T, JsError>
(see example here
at wasm_bindgen
documentation).
For a library with both a WASM build and a standard library build, however,
this could create a redundant API
with multiple #[wasm_bindgen]
functions that only wrap the internal function.
An alternative solution is to trivially implement Into<JsValue>
for the error type:
This crate simply wraps this code into a #[derive(JsError)]
macro for convenience.
Installation
Install with cargo add
:
Or by modifying Cargo.toml
:
[]
= "0.1"
Usage
Example with thiserror
:
use JsError;
use *;
use Error;
;
License
Licensed under either of Apache License (Version 2.0) or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.