Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Print unexpected panics to standard error #601

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ 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};

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() {
Expand Down Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you'd like to use ? here instead of expect we can add returning a Result from main to this PR in another commit! if not, that's OK :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can do that since set_hook wont accept a Result as a return type for the hook. It only accepts ().

}));
}
Ok(_) => {}
}
}