feat: Print unexpected panics to standard error

This will print to standard error for unexpected panics
and then let `human_panic` handle panics, just like before.
master
Jesper Håkansson 6 years ago
parent b07ec958d2
commit b329fefc84
  1. 32
      src/main.rs

@ -2,13 +2,13 @@ extern crate atty;
extern crate env_logger; extern crate env_logger;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
#[macro_use]
extern crate human_panic; extern crate human_panic;
extern crate structopt; extern crate structopt;
extern crate wasm_pack; extern crate wasm_pack;
extern crate which; extern crate which;
use std::env; use std::env;
use std::panic;
use structopt::StructOpt; use structopt::StructOpt;
use wasm_pack::{command::run_wasm_pack, Cli}; use wasm_pack::{command::run_wasm_pack, Cli};
@ -16,7 +16,9 @@ mod installer;
fn main() { fn main() {
env_logger::init(); env_logger::init();
setup_panic!();
setup_panic_hooks();
if let Err(e) = run() { if let Err(e) = run() {
eprintln!("Error: {}", e); eprintln!("Error: {}", e);
for cause in e.iter_causes() { for cause in e.iter_causes() {
@ -49,3 +51,29 @@ fn run() -> Result<(), failure::Error> {
run_wasm_pack(args.cmd)?; run_wasm_pack(args.cmd)?;
Ok(()) Ok(())
} }
fn setup_panic_hooks() {
let meta = human_panic::Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
};
let default_hook = panic::take_hook();
match env::var("RUST_BACKTRACE") {
Err(_) => {
panic::set_hook(Box::new(move |info: &panic::PanicInfo| {
// First call the default hook that prints to standard error.
default_hook(info);
// Then call human_panic.
let file_path = human_panic::handle_dump(&meta, info);
human_panic::print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
}
Ok(_) => {}
}
}

Loading…
Cancel
Save