webview_bundle_cli/
panic.rs

1use std::fmt::Write;
2use std::panic::{set_hook, PanicHookInfo};
3use std::thread;
4
5/// Installs a global panic handler to show a user-friendly error message in case the CLI panics
6pub fn setup_panic_handler() {
7  set_hook(Box::new(panic_handler))
8}
9
10fn panic_handler(info: &PanicHookInfo) {
11  // Buffer the error message to a string before printing it at once
12  // to prevent it from getting mixed with other errors if multiple threads
13  // panic at the same time
14  let mut error = String::new();
15
16  writeln!(error, "An unexpected error").unwrap();
17  writeln!(error).unwrap();
18
19  writeln!(error, "This is a bug, not an error in your code, and we would appreciate it if you could report it to https://github.com/seokju-na/webview-bundle/issues/ along with the following information to help us fixing the issue:").unwrap();
20  writeln!(error).unwrap();
21
22  if let Some(location) = info.location() {
23    writeln!(error, "Source Location: {location}").unwrap();
24  }
25
26  if let Some(thread) = thread::current().name() {
27    writeln!(error, "Thread Name: {thread}").unwrap();
28  }
29
30  let payload = info.payload();
31  if let Some(msg) = payload.downcast_ref::<&'static str>() {
32    writeln!(error, "Message: {msg}").unwrap();
33  } else if let Some(msg) = payload.downcast_ref::<String>() {
34    writeln!(error, "Message: {msg}").unwrap();
35  }
36
37  eprintln!("{error}");
38  tracing::error!("{error}");
39}