Skip to content

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

Closed
@cormacrelf

Description

@cormacrelf

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(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions