From 5035e96efe4ad0fad1b54b68aa7d1809b312c7b1 Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Thu, 5 May 2022 15:11:25 +0000 Subject: [PATCH 1/2] Expose panic formatting as function --- src/lib.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 89945d5..eefdf4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -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); } @@ -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) +} From 98e55d2d2a79066feac95996556460217a9c8448 Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Thu, 5 May 2022 15:56:03 +0000 Subject: [PATCH 2/2] Update Cargo.toml to reflect currently-released version. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index aa18e5c..d35037d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" }