tauri-docs icon indicating copy to clipboard operation
tauri-docs copied to clipboard

[docs] rust frontend crash when a command returns an error with current examples

Open dzamlo opened this issue 4 months ago • 2 comments

With the current examples, when invoking a command that return an error (a Err variant of a Result type) from a rust frontend, the rust frontend panic with an error about "unexpected exception".

The solution I found is to adjust the wasm_bindgen definition to the following:

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(catch, js_namespace = ["window", "__TAURI__", "core"])]
    async fn invoke(cmd: &str, args: JsValue) -> Result<JsValue, JsValue>;
}

And adjust the code using invoke to handle the result.

Maybe a better idea is to define two invoke external functions, one with the catch and one without, I don't know.

dzamlo avatar Aug 14 '25 14:08 dzamlo

Maybe a better idea is to define two invoke external functions, one with the catch and one without, I don't know.

If you have commands that don't return results it would indeed make sense https://github.com/JonasKruckenberg/tauri-sys/blob/v2/src/core.rs#L174-L177

FabianLars avatar Aug 14 '25 14:08 FabianLars

ngl i didn't even know we had some wasm instructions on our website https://v2.tauri.app/develop/calling-rust/#wasm

FabianLars avatar Aug 14 '25 14:08 FabianLars