Skip to content

ProspectPyxis/derive_jserror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

impl From<ErrorType> for JsValue {
    fn from(error: Error) -> Self {
        JsError::from(error).into()
    }
}

This crate simply wraps this code into a #[derive(JsError)] macro for convenience.

Installation

Install with cargo add:

Or by modifying Cargo.toml:

[dependencies]
derive_jserror = "0.1"

Usage

Example with thiserror:

use derive_jserror::JsError;
use wasm_bindgen::prelude::*;
use thiserror::Error;

#[derive(Error, JsError, Debug)]
#[error("test error type")]
struct TestError;

#[wasm_bindgen]
pub fn func_returns_result() -> Result<(), TestError> {
    Err(TestError)
}

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.

About

#[derive(JsError)]

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages