rustwasm / console_error_panic_hook Goto Github PK
View Code? Open in Web Editor NEWA panic hook for wasm32-unknown-unknown that logs panics with console.error
License: Apache License 2.0
A panic hook for wasm32-unknown-unknown that logs panics with console.error
License: Apache License 2.0
Crates.io lists 0.1.7 as the latest release, but there's no tag in the repo for that version.
The metadata points to commit f998b08b76655c16b948fc329c26ba563e9b3384 which also doesn't seem to be published. The only source difference from the current head (4dc30a5) seems to be the version bump.
git clone https://github.com/rustwasm/console_error_panic_hook
cd console_error_panic_hook
git show 0.1.7
unknown revision or path not in the working tree.
If applicable, add a link to a test case (as a zip file or link to a repository we can clone).
It's convenient for auditing changes if the source of each release is tagged in the official repository.
Could you please push the release commit and tag?
At the bottom of README Error.strackTraceLimit
should be changed to Error.stackTraceLimit
I have a workaround more or less; not for this issue exactly but it's just a way for me to keep working. Anyways I still wanted to ask out of curiosity.
I am trying to build a Vite plugin that loads a WASM module. I used the wasm-bindgen template as my starting point. In it, it provides a utils.rs
file which declares
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
Currently when I attempt to use the function, I unfortunately am unable to get any specific error message/issue output from the console. I still see the general "unreachable" error and then a stack trace pointing to a memory location in the WASM module.
Even with a very simple test case like
pub fn process_file(){
set_panic_hook();
panic!("test");
}
I still get the general "unreachable" message.
Not sure if it might be the issue but maybe the issue lies somewhere in the interaction with Vite?
Thanks!
Currently panics are printed to the dev console. However, the errors are not manifesting in the web UI. It is not a great user experience.
I understand that the goal of the crate is to print to console, however I am looking for ways to pass the panic info from panic hook into my web application. I run a WebAssembly module in a WebWorker. What are my options?
Is there a way to emit a DOM event from the panic handler somehow and use e.g. AddEventListener()
to subscribe to it?
What are the options for a WebAssembly module running in a WebWorker? (it does not have window
and some of the other functionality compared to the main thread).
I am using React for UI, Recoil for state handling, and Observable for events.
Any thoughts or ideas?
I am looking for possible solutions yet.
I am looking for possible alternatives yet.
N/A
As soon as I start my app I get the following in the console and a broken app:
Uncaught (in promise) TypeError: Import #5 module="__wbindgen_placeholder__" error: module is not an object or function
When I try to use console_error_panic_hook
in my yew project, my app panics on std::panic::set_hook(box console_error_panic_hook::hook)
with:
Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #7 module="wbindgen_placeholder" error: module is not an object or function
I have the following [dependencies]
:
yew = "0.11"
wasm-bindgen = "0.2"
console_error_panic_hook = "0.1.6"
#[wasm_bindgen(start)]
pub fn main_js() {
#[cfg(debug_assertions)]
std::panic::set_hook(box console_error_panic_hook::hook);
web_logger::init();
yew::initialize();
App::<Model>::new().mount_to_body();
yew::run_loop();
}
fn main() {
main_js();
}
Currently, errors are logged to the console using console.error
. However, it would be useful to provide your own callback for the message, which would allow for using tracing::error
instead, which would also enable panics to show in wasm-pack test --node
, as well as making it possible for providing your own error screens
Provide a new setup function which takes a closure to invoke with the error message, and make the existing set_once
use this internally by falling back to console.error
console_error_panic_hook::set_once_with(|msg| tracing::error!("{msg}"));
Making a custom panic hook in place which will contain almost all of this crate's logic, as I have currently done to enable this to work with wasm-pack test
and non console environments
I'm defining my initalization function like so:
#[wasm_bindgen]
pub fn init() {
#[cfg(feature = "console_error_panic_hook")]
panic::set_hook(Box::new(console_error_panic_hook::hook));
//set_panic_hook();
wasm_logger::init(wasm_logger::Config::default());
}
This gives the following error when running wasm-pack build --release
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `"Error"`,
right: `"Error2"`', crates\cli-support\src\js\mod.rs:175:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit code: 101
full command: "C:\\Users\\brand\\AppData\\Local\\.wasm-pack\\wasm-bindgen-7e757a9923d43b8a\\wasm-bindgen.exe" "C:\\Users\\brand\\repos\\celeste\\satellite-rs\\target\\wasm32-unknown-unknown\\release\\satellite_rs.wasm" "--out-dir" "C:\\Users\\brand\\repos\\celeste\\satellite-rs\\pkg" "--typescript" "--target" "bundler"
this error does not occur if I disable the "console_error_panic_hook" feature
I would expect that I would be able to create a wasm-bindgen function that I can call from my frontend to initalize the logging hook
Got the following error on wasm-pack build:
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `"Error"`,
right: `"Error2"`', crates\cli-support\src\js\mod.rs:175:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit code: 101
full command: "C:\\Users\\brand\\AppData\\Local\\.wasm-pack\\wasm-bindgen-7e757a9923d43b8a\\wasm-bindgen.exe" "C:\\Users\\brand\\repos\\celeste\\satellite-rs\\target\\wasm32-unknown-unknown\\release\\satellite_rs.wasm" "--out-dir" "C:\\Users\\brand\\repos\\celeste\\satellite-rs\\pkg" "--typescript" "--target" "bundler"
So users get an idea what to expect.
What I see in Firefox:
What I see in Safari:
Edit:
wasm-bindgen generates this code:
const __wbg_error_29632ca09755cc79_target = console.error;
let cachedDecoder = new TextDecoder('utf-8');
// ...
export function __wbg_error_29632ca09755cc79(arg0, arg1) {
let varg0 = getStringFromWasm(arg0, arg1);
__wbg_error_29632ca09755cc79_target(varg0);
}
If this line:
__wbg_error_29632ca09755cc79_target(varg0);
is manually changed to this:
__wbg_error_29632ca09755cc79_target(varg0 + " ");
then it works. It seems that the string literal has to have a space in it, an empty string literal doesn't work.
I think this can be text rather than a screenshot like in #1 since node just has a REPL and no GUI.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.