Skip to content

Instantly share code, notes, and snippets.

@ktwrd
Created January 11, 2025 04:04
Show Gist options
  • Save ktwrd/bd1e7a9bc43a613030b8fdcd5f693b40 to your computer and use it in GitHub Desktop.
Save ktwrd/bd1e7a9bc43a613030b8fdcd5f693b40 to your computer and use it in GitHub Desktop.
Get Panic Message
// Used to get the panic message after PR 115974 was merged, which changed the type passed through
// in the "info" parameter in the "std::panic::set_hook" callback/delegate. In the new type that
// is passed through, the "message" field was removed. To get the message back from the panic info,
// use the "payload_message" function.
use std::panic::PanicHookInfo;
fn main() {
std::panic::set_hook(Box::new(move |info| {
println!("Panic Info: {:#?}", info);
println!("{:#?}", payload_message(info));
}));
panic!("Lorem Ipsum");
}
fn payload_message(info: &PanicHookInfo) -> String {
if let Some(s) = info.payload().downcast_ref::<&str>() {
String::from(*s)
} else if let Some(s) = info.payload().downcast_ref::<String>() {
s.clone()
} else {
String::from("<unknown error> (unhandled downcast_ref in payload_message)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment