Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't return Err(js_sys::Error)? in -> Result<(), JsValue> function #1785

Closed
cormacrelf opened this issue Sep 24, 2019 · 2 comments · Fixed by #1788
Closed

Can't return Err(js_sys::Error)? in -> Result<(), JsValue> function #1785

cormacrelf opened this issue Sep 24, 2019 · 2 comments · Fixed by #1788

Comments

@cormacrelf
Copy link
Contributor

cormacrelf commented Sep 24, 2019

Creating a js_sys::Error and converting it to a JsValue will erroneously cause the glue code to catch the error you're trying to create. (Bit weird, catch shouldn't catch an Error when it's just a return value from a new Error() call.) I just get this in the console, printed twice.

wasm-bindgen: imported JS function that was not marked as `catch` threw an error: message goes here

Stack:
__wbg_new_2e9dc2ca6bd84218@webpack-internal:///./pkg/index.js:107:21
__wbg_new_2e9dc2ca6bd84218@http://localhost:8080/index.js:68:66
js_sys::Error::new::h7c642be162d2b294@http://localhost:8080/15b9d87f0cb69b4970c6.module.wasm:wasm-function[90]:0x7b67
rust_webpack_template::tries_to_throw::hec9794da8aa9f021@http://localhost:8080/15b9d87f0cb69b4970c6.module.wasm:wasm-function[17]:0x2adb
tries_to_throw@http://localhost:8080/15b9d87f0cb69b4970c6.module.wasm:wasm-function[132]:0x8f2f
tries_to_throw@webpack-internal:///./pkg/index.js:27:53
@webpack-internal:///./js/index.js:2:7

Originally posted by @cormacrelf in #1742 (comment)

Repro:

use js_sys::Error;
use wasm_bindgen::prelude::*;
use web_sys::console;
#[wasm_bindgen]
pub extern "C" fn tries_to_throw() -> Result<(), JsValue> {
    console::log_1(&JsValue::from_str("we made it this far"));
    let _z = Err(Error::new("message goes here"))?;
    Ok(())
}
@Kirguir
Copy link
Contributor

Kirguir commented Sep 24, 2019

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Sep 24, 2019
To benefit users in debug mode we log any unexpected exceptions to help
diagnose any issues that might arise. It turns out, though, we log this
for *every* exception happening for *every* import, including imports
like `__wbindgen_throw` which are explicitly intended to throw an
exception. This can cause distracting debug logs to get emitted to the
console, so let's squelch the debug logging for known imports that we
shouldn't log for, such as intrinsics.

Closes rustwasm#1785
@alexcrichton
Copy link
Contributor

Thanks for splitting this out! I believe this is fixed by #1788 where we were erroneously logging errors as unexpected where they were actually expected.

alexcrichton added a commit that referenced this issue Sep 25, 2019
To benefit users in debug mode we log any unexpected exceptions to help
diagnose any issues that might arise. It turns out, though, we log this
for *every* exception happening for *every* import, including imports
like `__wbindgen_throw` which are explicitly intended to throw an
exception. This can cause distracting debug logs to get emitted to the
console, so let's squelch the debug logging for known imports that we
shouldn't log for, such as intrinsics.

Closes #1785
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants