#console #panic #error #hook #logs #debug

console_error_panic_hook

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

7 releases

Uses old Rust 2015

0.1.6 Feb 15, 2019
0.1.5 Sep 6, 2018
0.1.4 Aug 14, 2018
0.1.3 Jul 19, 2018
0.1.1 May 23, 2018

#67 in WebAssembly

Download history 26838/week @ 2021-06-07 27302/week @ 2021-06-14 28977/week @ 2021-06-21 28243/week @ 2021-06-28 29397/week @ 2021-07-05 30148/week @ 2021-07-12 31307/week @ 2021-07-19 33203/week @ 2021-07-26 31986/week @ 2021-08-02 38577/week @ 2021-08-09 31867/week @ 2021-08-16 30826/week @ 2021-08-23 31804/week @ 2021-08-30 31401/week @ 2021-09-06 36227/week @ 2021-09-13 38626/week @ 2021-09-20

135,034 downloads per month
Used in 548 crates (141 directly)

Apache-2.0/MIT

505KB

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

Dependencies

~0.8–1.3MB
~28K SLoC