Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "Apache-2.0/MIT"
name = "console_error_panic_hook"
readme = "./README.md"
repository = "https://github.com/rustwasm/console_error_panic_hook"
version = "0.1.6"
version = "0.1.7"

[badges]
travis-ci = { repository = "rustwasm/console_error_panic_hook" }
Expand Down
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ cfg_if! {
fn stack(error: &Error) -> String;
}

fn hook_impl(info: &panic::PanicInfo) {
fn format_panic_impl(info: &panic::PanicInfo) -> String {
let mut msg = info.to_string();

// Add the error stack to our message.
Expand All @@ -118,12 +118,22 @@ cfg_if! {
// https://github.com/rustwasm/console_error_panic_hook/issues/7
msg.push_str("\n\n");

// Finally, log the panic with `console.error`!
msg
}

fn hook_impl(info: &panic::PanicInfo) {
let msg = format_panic(info);

// Log the panic with `console.error`
error(msg);
}
} else {
use std::io::{self, Write};

fn format_panic_impl(info: &panic::PanicInfo) -> String {
info.to_string()
}

fn hook_impl(info: &panic::PanicInfo) {
let _ = writeln!(io::stderr(), "{}", info);
}
Expand All @@ -150,3 +160,9 @@ pub fn set_once() {
panic::set_hook(Box::new(hook));
});
}

/// Format panic info for display with `console.error'. This is a low-level function for use by
/// custom panic hooks.
pub fn format_panic(info: &panic::PanicInfo) -> String {
format_panic_impl(info)
}