Crates.io | console_error_panic_hook |
lib.rs | console_error_panic_hook |
version | 0.1.7 |
source | src |
created_at | 2018-05-23 22:46:11.047945 |
updated_at | 2021-10-11 17:01:02.936833 |
description | A panic hook for `wasm32-unknown-unknown` that logs panics to `console.error` |
homepage | |
repository | https://github.com/rustwasm/console_error_panic_hook |
max_upload_size | |
id | 66839 |
size | 541,966 |
console_error_panic_hook
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:
Node:
With this panic hook installed you will see the panic message
Browser:
Node:
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
}
}
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.