Code Monkey home page Code Monkey logo

console_error_panic_hook's Introduction

console_error_panic_hook

Build Status

This crate lets you debug panics on wasm32-unknown-unknown by providing a panic hook that forwards panic messages to console.error.

When an error is reported with console.error, browser devtools and node.js will typically capture a stack trace and display it with the logged error message.

Without console_error_panic_hook you just get something like RuntimeError: Unreachable executed

Browser: Console without panic hook

Node: Node console without panic hook

With this panic hook installed you will see the panic message

Browser: Console with panic hook set up

Node: Node console with panic hook set up

Usage

There are two ways to install this panic hook.

First, you can set the hook yourself by calling std::panic::set_hook in some initialization function:

extern crate console_error_panic_hook;
use std::panic;

fn my_init_function() {
    panic::set_hook(Box::new(console_error_panic_hook::hook));

    // ...
}

Alternatively, use set_once on some common code path to ensure that set_hook is called, but only the one time. Under the hood, this uses std::sync::Once.

extern crate console_error_panic_hook;

struct MyBigThing;

impl MyBigThing {
    pub fn new() -> MyBigThing {
        console_error_panic_hook::set_once();

        MyBigThing
    }
}

Error.stackTraceLimit

Many browsers only capture the top 10 frames of a stack trace. In rust programs this is less likely to be enough. To see more frames, you can set the non-standard value Error.stackTraceLimit. For more information see the MDN Web Docs or v8 docs.

console_error_panic_hook's People

Contributors

alexcrichton avatar coolreader18 avatar dependabot-preview[bot] avatar fitzgen avatar hamchapman avatar itsybitesyspider avatar kylejlin avatar migerh avatar yisonpylkita avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

console_error_panic_hook's Issues

Display panic error messages in web UI

Motivation

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?

Proposed Solution

I am looking for possible solutions yet.

Alternatives

I am looking for possible alternatives yet.

Additional Context

N/A

Setting panic hook in initialization function causes wasm-pack to break

Describe the Bug

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

Steps to Reproduce

  1. See error

Expected Behavior

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

Actual Behavior

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"

Sanity check - am I using this crate correctly? Doesn't seem to work for me for some reason when attempting to use with Vite

Summary

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.

Additional Details

Not sure if it might be the issue but maybe the issue lies somewhere in the interaction with Vite?

Thanks!

Customize error message reporting

Motivation

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

Proposed Solution

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}"));

Alternatives

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

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #7 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();
}

Typo in README

Describe the Bug

At the bottom of README Error.strackTraceLimit should be changed to Error.stackTraceLimit

customer panicinfo info

  • in release we do not want to show full compilation file path from panic info
  • because it reveals interanals
  • can we disable it, may be via some feature gate?

Doesn't work in Safari

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.

Source commit and tag for 0.1.7 missing

Describe the Bug

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.

Steps to Reproduce

  1. Go to https://crates.io/crates/console_error_panic_hook/versions
  2. Obvserve published versions
  3. git clone https://github.com/rustwasm/console_error_panic_hook
  4. cd console_error_panic_hook
  5. git show 0.1.7
  6. See error 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).

Expected Behavior

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?

Error when used with `cargo web`

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.