console_error_panic_hook

A panic hook for wasm32-unknown-unknown that logs panics to console.error

6 releases

0.1.5 Sep 6, 2018
0.1.4 Aug 14, 2018
0.1.3 Jul 19, 2018
0.1.1 May 23, 2018

#13 in WebAssembly

Download history 16/week @ 2018-06-14 8/week @ 2018-06-21 26/week @ 2018-06-28 25/week @ 2018-07-05 17/week @ 2018-07-12 42/week @ 2018-07-19 131/week @ 2018-07-26 275/week @ 2018-08-02 682/week @ 2018-08-09 599/week @ 2018-08-16 426/week @ 2018-08-23 441/week @ 2018-08-30 481/week @ 2018-09-06

1,055 downloads per month
Used in 7 crates (1 directly)

Apache-2.0/MIT

107KB


lib.rs:

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

Console without panic hook

With this panic hook installed you will see the panic message

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

Dependencies

~2MB
~42K SLoC