Merge pull request #601 from drager/unexpected-panics

feat: Print unexpected panics to standard error
master
ashley williams 6 years ago committed by GitHub
commit 67089e0a55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      src/main.rs

@ -2,13 +2,13 @@ extern crate atty;
extern crate env_logger;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate human_panic;
extern crate structopt;
extern crate wasm_pack;
extern crate which;
use std::env;
use std::panic;
use structopt::StructOpt;
use wasm_pack::{command::run_wasm_pack, Cli};
@ -16,7 +16,9 @@ mod installer;
fn main() {
env_logger::init();
setup_panic!();
setup_panic_hooks();
if let Err(e) = run() {
eprintln!("Error: {}", e);
for cause in e.iter_causes() {
@ -49,3 +51,29 @@ fn run() -> Result<(), failure::Error> {
run_wasm_pack(args.cmd)?;
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