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

Overview

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.

Comments
  • Doesn't work in Safari

    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.

    opened by kzvi 7
  • Update readme to talk about Error.stackTraceLimit

    Update readme to talk about Error.stackTraceLimit

    Stack traces from rust, including the example screenshots in the existing documentation (!), are often so full of stack frames from the panic mechanism itself and other generic platform functions that there is not enough information to identify where the error was triggered in user code. Hopefully this documentation will save time for the next person who has this problem.

    opened by itsybitesyspider 2
  • Expose panic formatting as function

    Expose panic formatting as function

    Enso developers would like to implement a panic hook that writes to console.error, but also uses an application-specific API to write the panic info to the console running the program as an electron app. The latter is straightforward enough, but console_error_panic_hook's formatting addresses browser subtleties, and we'd prefer not to duplicate that.

    This PR exposes panic formatting in the API, for use from custom panic hooks.

    opened by kazcw 1
  • Error when used with `cargo web`

    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
    
    opened by estk 1
  • Use the new feature flag for procedural macros

    Use the new feature flag for procedural macros

    See https://github.com/rustwasm/wasm-bindgen/commit/ed05c7b9454905f15488714619950a55cd3c8d2d

    Apparently the feature flag for proc macros was renamed to use_extern_macros.

    opened by migerh 1
  • Upgrade to GitHub-native Dependabot

    Upgrade to GitHub-native Dependabot

    Dependabot Preview will be shut down on August 3rd, 2021. In order to keep getting Dependabot updates, please merge this PR and migrate to GitHub-native Dependabot before then.

    Dependabot has been fully integrated into GitHub, so you no longer have to install and manage a separate app. This pull request migrates your configuration from Dependabot.com to a config file, using the new syntax. When merged, we'll swap out dependabot-preview (me) for a new dependabot app, and you'll be all set!

    With this change, you'll now use the Dependabot page in GitHub, rather than the Dependabot dashboard, to monitor your version updates, and you'll configure Dependabot through the new config file rather than a UI.

    You have configured automerging on this repository. There is no automerging support in GitHub-native Dependabot, so these settings will not be added to the new config file. Several 3rd-party GitHub Actions and bots can replicate the automerge feature.

    If you've got any questions or feedback for us, please let us know by creating an issue in the dependabot/dependabot-core repository.

    Learn more about migrating to GitHub-native Dependabot

    Please note that regular @dependabot commands do not work on this pull request.

    dependencies 
    opened by dependabot-preview[bot] 0
  • Make FireFox Put the @URL Part on Its Own Column

    Make FireFox Put the @URL Part on Its Own Column

    On Firefox 72.0.2, noticed that the URL part of the stack trace isn't lined up like in the README. So manually added some periods to space things out.

    Here's an example of what I'm seeing:

    Stack:
    
    init/imports.wbg.__wbg_new_59cb74e423758ede@http://localhost:1234/rust.js:303:23
    console_error_panic_hook::Error::new::h21c6f8c021401ca7@http://localhost:1234/rust.wasm:wasm-function[1312]:0x7004a
    console_error_panic_hook::hook_impl::h51d369253f11867c@http://localhost:1234/rust.wasm:wasm-function[240]:0x49b09
    console_error_panic_hook::hook::h7240c976685d8fda@http://localhost:1234/rust.wasm:wasm-function[1407]:0x7179f
    core::ops::function::Fn::call::hd089ec940b4dcbbd@http://localhost:1234/rust.wasm:wasm-function[1318]:0x701f6
    std::panicking::rust_panic_with_hook::h4e529e530989255b@http://localhost:1234/rust.wasm:wasm-function[372]:0x543e3
    std::panicking::begin_panic::h1f5bf870a56b785e@http://localhost:1234/rust.wasm:wasm-function[512]:0x5b698
    BugCraft::game::Game::update::h168b1700ce7fdc84@http://localhost:1234/rust.wasm:wasm-function[16]:0xcd4d
    BugCraft::update::hc4c0739190f6c8fa@http://localhost:1234/rust.wasm:wasm-function[1315]:0x70131
    update@http://localhost:1234/rust.wasm:wasm-function[1103]:0x6c350
    _on_update@http://localhost:1234/typescript.js:341:28
    

    Testing

    Verified cargo test still passes, that this doesn't affect Chrome, and that Firefox now looks like:

    Stack:
    
    init/imports.wbg.__wbg_new_59cb74e423758ede ............. @http://localhost:1234/rust.js:303:23
    console_error_panic_hook::Error::new::hbe0bf58356f924b2 . @http://localhost:1234/rust.wasm:wasm-function[1405]:0x781b7
    console_error_panic_hook::hook_impl::ha653d0da3dbebdd3 .. @http://localhost:1234/rust.wasm:wasm-function[38]:0x24828
    console_error_panic_hook::hook::h4e971616aff3a927 ....... @http://localhost:1234/rust.wasm:wasm-function[1510]:0x79b76
    core::ops::function::Fn::call::h8f08f944b67fcc08 ........ @http://localhost:1234/rust.wasm:wasm-function[1411]:0x78363
    std::panicking::rust_panic_with_hook::h4e529e530989255b . @http://localhost:1234/rust.wasm:wasm-function[407]:0x5a67b
    std::panicking::begin_panic::h1f5bf870a56b785e .......... @http://localhost:1234/rust.wasm:wasm-function[561]:0x624d2
    BugCraft::game::Game::update::h168b1700ce7fdc84 ......... @http://localhost:1234/rust.wasm:wasm-function[16]:0xcdc3
    BugCraft::update::hc4c0739190f6c8fa ..................... @http://localhost:1234/rust.wasm:wasm-function[1408]:0x7829e
    update .................................................. @http://localhost:1234/rust.wasm:wasm-function[1188]:0x74269
    _on_update .............................................. @http://localhost:1234/typescript.js:341:28
    
    opened by aaron-human 0
  • Add example error message and backtrace from node

    Add example error message and backtrace from node

    I wasn't sure where you wanted me to put the error messages, so I put them in .txt files.

    For the purposes of reproduction, I generated the error messages from here: https://github.com/kylejlin/console-error-panic-hook-test

    opened by kylejlin 0
  • Expose a function to stringify a PanicInfo struct

    Expose a function to stringify a PanicInfo struct

    This allows users who want to display the error message generated by console_error_panic_hook outside of the console to do it without having to reimplement javascript-based stack walking themselves.


    Hey! I'm in the process of trying to be nicer to users upon panic, displaying an error page on my yew app instead of just crashing and dumping logs to the console (source). However, I'd rather avoid reimplementing the stack walking that console_error_panic_hook implemented in order to tell the user what they should copy-paste to the issue tracker.

    So I thought of just exposing it from console_error_panic_hook, though it may be slightly outside of the expected API surface for it.

    WDYT?

    opened by Ekleog 0
  • Expose panic formatting in API

    Expose panic formatting in API

    Hi, I'm working on an Electron application, Enso. Enso needs to do multiple things in our panic hook: We have an application-specific API to submit the backtrace to, but we'd also like to display it with console.error. We want to minimize duplication of the functionality defined in console_error_panic_hook--especially the backtrace formatting, since it addresses browser subtleties.

    This proposed change introduces a lower level function to the API, exposing the panic formatting functionality for use from another hook.

    (Also, it increments the release version in Cargo.toml to match the currently-released version.)

    opened by kazcw 1
  • Source commit and tag for 0.1.7 missing

    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 (4dc30a5448ed3ffcfb961b1ad54d000cca881b84) 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?

    bug 
    opened by rillian 1
  • customer panicinfo info

    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?
    enhancement 
    opened by dzmitry-lahoda 0
  • Update cfg-if requirement from 0.1.6 to 1.0.0

    Update cfg-if requirement from 0.1.6 to 1.0.0

    ⚠️ Dependabot Preview has been deactivated ⚠️

    This pull request was created by Dependabot Preview, and you've upgraded to Dependabot. This means it won't respond to dependabot commands nor will it be automatically closed if a new version is found.

    If you close this pull request, Dependabot will re-create it the next time it checks for updates and everything will work as expected.


    Updates the requirements on cfg-if to permit the latest version.

    Commits

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.

    If all status checks pass Dependabot will automatically merge this pull request.


    Note: This repo was added to Dependabot recently, so you'll receive a maximum of 5 PRs for your first few update runs. Once an update run creates fewer than 5 PRs we'll remove that limit.

    You can always request more updates by clicking Bump now in your Dependabot dashboard.

    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Automerge options (never/patch/minor, and dev/runtime dependencies)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
    dependencies 
    opened by dependabot-preview[bot] 2
Owner
Rust and WebAssembly
🦀 + 🕸️ = 💖
Rust and WebAssembly
Bam Error Stats Tool (best): analysis of error types in aligned reads.

best Bam Error Stats Tool (best): analysis of error types in aligned reads. best is used to assess the quality of reads after aligning them to a refer

Google 54 Jan 3, 2023
This library provides a convenient derive macro for the standard library's std::error::Error trait.

derive(Error) This library provides a convenient derive macro for the standard library's std::error::Error trait. [dependencies] therror = "1.0" Compi

Sebastian Thiel 5 Oct 23, 2023
😱 Panic messages for humans.

human-panic Panic messages for humans. Handles panics by calling std::panic::set_hook to make errors nice for humans. Documentation Crates.io Why? Whe

Rust CLI WG 997 Jan 1, 2023
Scouty is a command-line interface (CLI) to keep an eye on substrate-based chains and hook things up

scouty is a command-line interface (CLI) to keep an eye on substrate-based chains and hook things up

TurboFlakes 15 Aug 6, 2022
Pre-commit hook to help me stop naming files *.yml half of the time

Disallow file endings Pre-commit hook that lets you specify banned file endings. I keep naming half my yaml files *.yaml and the other *.yml and it's

Sondre Lillebø Gundersen 4 Mar 8, 2023
⚙️ Pre-commit hook for downgrading Python logger syntax

printf-log-formatter Automatically convert f-strings and str.format() syntax to printf-style strings. In other words, this syntax logger.error(f"{1}")

snok 5 Apr 25, 2023
try to find the correct word with only first letter and unknown letter count.

MOTUS Current dictionaries are provided in french and can contain some words not included in the official Motus dictionary. Additionally, dictionaries

Alexandre 6 Apr 11, 2022
Command-line program to manage PS battle logs. WIP.

psbattletools psbattletools is a command-line tool written in Rust for manipulating Pokémon Showdown battle logs. Installation psbattletools currently

Annika 2 Dec 27, 2022
Infino - Fast and scalable service to store time series and logs - written in Rust

Infino 〽️ ?? — ?? ?? — ⚖️ ?? Ingest Metrics and Logs — Query and Insights — Scale and Save $$ Infino is an observability platform for storing metrics

null 6 Apr 23, 2023
A fast, simple TUI for interacting with systemd services and their logs

systemctl-tui A fast, simple TUI for interacting with systemd services and their logs. systemctl-tui can quickly browse service status and logs, and s

Reilly Wood 11 Sep 1, 2023
Console progress bar for Rust

Terminal progress bar for Rust Console progress bar for Rust Inspired from pb, support and tested on MacOS, Linux and Windows Documentation Examples s

Ariel Mashraki 507 Dec 22, 2022
🌈 Brings back colour console to Win64 for Garry's Mod SRCDS

?? gmsv_concolormsg This module for Garry's Mod fixes x86-64 Windows 64-bit SRCDS not displaying colours. Why does it do that? Who knows! But it's eas

William 11 Oct 4, 2022
Simple console input macros with the goal of being implemented in the standard library.

Simple console input macros with the goal of being implemented in the standard library.

undersquire 2 Feb 10, 2022
Tldr - 📚 Collaborative cheatsheets for console commands

What is tldr-pages? The tldr-pages project is a collection of community-maintained help pages for command-line tools, that aims to be a simpler, more

tldr pages 42.4k Dec 29, 2022
Wena is a micro-framework that provides an elegant starting point for your console application.

Wena was created by Nuno Maduro, and is a Rust Lang micro-framework that provides an elegant starting point for your console application. This project

null 251 Dec 11, 2022
Rust Crate that allows to do interruptions in console. Will be implemented to functional terminal customization kit.

termpause Rust Crate that allows to do interruptions in console. Will be implemented to functional terminal customization kit. Usage Add this in your

Just Said 4 Sep 21, 2022
An ascii webcam in your console, written as a way of learning rust.

asciicam An ascii webcam in your console, written as a way of learning rust. asciicam picture of me holding a basketball usage only linux is supported

Vilhelm Bergsøe 3 Nov 15, 2022
scan markdown files and execute `console` blocks

exec-commands − scan markdown files and execute console blocks exec-commands is a utility to update command-line-tool examples embedded in markdown fi

Hajime Suzuki 3 Nov 27, 2022
Logging for text that should stay in the same place in a console.

console_static_text Crate for logging text that should stay in the same place in a console. This measures words to handle wrapping and has some consol

David Sherret 8 Dec 25, 2022