madonoharu / tsify

A library for generating TypeScript definitions from rust code.
Apache License 2.0
300 stars 41 forks source link

Propagate serde error message to JS exception #19

Closed steos closed 1 year ago

steos commented 1 year ago

When deserializing a tsified struct from a JS value the serde error message gets lost. With this change the error message gets propagated as the JS exception message.

Consider this example:

#[derive(Debug, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Foo {
    foo: String
}

#[wasm_bindgen]
pub fn foobar(params: Foo) -> Result<String, JsError> {
    Ok(String::from("hello"))
}

When calling foobar from JS with invalid Foo

before:

foobar()
// Uncaught Error: `unwrap_throw` failed

foobar({})
// Uncaught Error: `unwrap_throw` failed

after:

foobar()
// Uncaught Error: Error: invalid type: unit value, expected struct Foo

foobar({})
// Uncaught Error: Error: missing field `foo`
madonoharu commented 1 year ago

Sorry for the late reply. Thank you!