Skip to content

Commit

Permalink
Only display message for humans (implementing #48) (#50)
Browse files Browse the repository at this point in the history
* Implementing #48 
* Running rustfmt on the new setup

There will be another commit that adds more documentation for new 1.0+ features across the board
  • Loading branch information
spacekookie authored Oct 4, 2018
2 parents 920251c + b428118 commit 80867e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
52 changes: 33 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,48 @@ pub struct Metadata {
#[macro_export]
macro_rules! setup_panic {
($meta:expr) => {
#[allow(unused_imports)]
use std::panic::{self, PanicInfo};
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);

print_msg(file_path, &$meta)
.expect("human-panic: printing error message to console failed");
}));
#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&$meta, info);
print_msg(file_path, &$meta)
.expect("human-panic: printing error message to console failed");
}));
}
Ok(_) => {}
}
};

() => {
#[allow(unused_imports)]
use std::panic::{self, PanicInfo};
#[allow(unused_imports)]
use $crate::{handle_dump, print_msg, Metadata};

let meta = 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(),
};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);

print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
#[cfg(not(debug_assertions))]
match ::std::env::var("RUST_BACKTRACE") {
Err(_) => {
let meta = 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(),
};

panic::set_hook(Box::new(move |info: &PanicInfo| {
let file_path = handle_dump(&meta, info);
print_msg(file_path, &meta)
.expect("human-panic: printing error message to console failed");
}));
}
Ok(_) => {}
}
};
}

Expand Down
13 changes: 11 additions & 2 deletions tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate assert_cli;

#[test]
fn integration() {
assert_cli::Assert::main_binary()
fn release() {
assert_cli::Assert::command(&["cargo", "run", "--release"])
.stderr()
.contains("custom-panic-test")
.stderr()
Expand All @@ -14,3 +14,12 @@ fn integration() {
.fails_with(101)
.unwrap();
}

#[test]
fn debug() {
assert_cli::Assert::command(&["cargo", "run"])
.stderr()
.contains("OMG EVERYTHING IS ON FIRE!!!")
.fails_with(101)
.unwrap();
}
13 changes: 11 additions & 2 deletions tests/single-panic/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate assert_cli;

#[test]
fn integration() {
assert_cli::Assert::main_binary()
fn release() {
assert_cli::Assert::command(&["cargo", "run", "--release"])
.stderr()
.contains("single-panic-test")
.stderr()
Expand All @@ -12,3 +12,12 @@ fn integration() {
.fails_with(101)
.unwrap();
}

#[test]
fn debug() {
assert_cli::Assert::command(&["cargo", "run"])
.stderr()
.contains("OMG EVERYTHING IS ON FIRE!!!")
.fails_with(101)
.unwrap();
}

0 comments on commit 80867e1

Please sign in to comment.