neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
8k stars 283 forks source link

JsError doesn't work on non-wasm targets #999

Closed kristian1108 closed 1 year ago

kristian1108 commented 1 year ago

I'm new to this wasm ecosystem so apologies if this is a silly issue/question.

I'm trying to test my wasm module inside the Rust project, the same way I would do with a normal Rust crate. I'm running into an issue where I can't pass errors outside of the wasm functions that are compatible with my local architecture. For example:

#[wasm_bindgen]
pub fn foo(bar: &str) -> Result<(), JsError> {}

If this method returns an error, it will panic my tests with:

thread 'server::auth_test::test_activate_user_v2' panicked at 'function not implemented on non-wasm32 targets', /Users/kristian/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-0.2.87/src/lib.rs:996:1

After tracing through the code, I discovered this is originating in the JsError::new() call. You cannot instantiate a JSError outside of a wasm architecture.

Is there a way to get around this so I can pass helpful errors back in my test code?

One solution is to dynamically compile in a different error. For example:

#[cfg(target_arch = "wasm32")]
type WasmError = JsError;

#[cfg(not(target_arch = "wasm32"))]
type WasmError = anyhow::Error;

#[wasm_bindgen]
pub fn foo(bar: &str) -> Result<(), WasmError> {}

But this seems like a lot of headache for what I have to think is a very common problem? Thanks for your help!