rustwasm / gloo

A modular toolkit for building fast, reliable Web applications and libraries with Rust and WASM
https://gloo-rs.web.app
Apache License 2.0
1.72k stars 141 forks source link

TryFrom<JsValue> for JsError panics in the NotJsError case if the JsValue isn't a string #477

Open cs2dsb opened 1 month ago

cs2dsb commented 1 month ago

Describe the Bug

The library I'm calling (sqlite3.js) returns an object with "type": "Error" instead of an instance of Error or a String. Calling try_from panics because the fallback case calls JsString::from which panics if the type is wrong. It should probably call try_from and fallback to calling fmt::Debug or similar in the non-string case. I can work around it but the try_from panicking seems unexpected.

Here is the relevant line: https://github.com/rustwasm/gloo/blob/dc0e61e7b24fe0f33d66960385909dc0f4195bd9/crates/utils/src/errors.rs#L55

Steps to Reproduce

Reject a promise with an object and call JsError::try_from on the resulting JsValue.

Expected Behavior

I don't think try_from should ever panic. Making the string on NotJsError optional or making the error type an enum would make more sense to me.

Actual Behavior

Panics in JsString::from with "unwrap_throw failed".

Additional Context