/human-panic

😱 Panic messages for humans.

Primary LanguageRustApache License 2.0Apache-2.0

human-panic

crates.io version build status downloads docs.rs docs

Panic messages for humans. Handles panics by calling std::panic::set_hook to make errors nice for humans.

Why?

When you're building a CLI, polish is super important. Even though Rust is pretty great at safety, it's not unheard of to access the wrong index in a vector or have an assert fail somewhere.

When an error eventually occurs, you probably will want to know about it. So instead of just providing an error message on the command line, we can create a call to action for people to submit a report.

This should empower people to engage in communication, lowering the chances people might get frustrated. And making it easier to figure out what might be causing bugs.

Default Output

thread 'main' panicked at 'oops', examples/main.rs:2:3
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Human-Panic Output

Well, this is embarrassing.

human-panic had a problem and crashed. To help us diagnose the problem you can send us a crash report.

We have generated a report file at "/var/folders/zw/bpfvmq390lv2c6gn_6byyv0w0000gn/T/report-8351cad6-d2b5-4fe8-accd-1fcbf4538792.toml". Submit an issue or email with the subject of "human-panic Crash Report" and include the report as an attachment.

- Homepage: https://github.com/yoshuawuyts/human-panic
- Authors: Yoshua Wuyts <yoshuawuyts@gmail.com>

We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.

Thank you kindly!

The error dump file generated by human-panic contains the following fields.

name = 'single-panic-test2'
operating_system = 'Debian (11) (64-bit)'
crate_version = '0.1.0'
explanation = '''
Panic occurred in file 'src/main.rs' at line 7
'''
cause = 'OMG EVERYTHING IS ON FIRE!!!'
method = 'Panic'
backtrace = '''

   0: 0x55f1b1ed42af - single_panic_test2::main::h71eb4bc2300956b7
   1: 0x55f1b1ed3623 - std::sys_common::backtrace::__rust_begin_short_backtrace::hb897a2290e3a3ed8
   2: 0x55f1b1ed35f9 - std::rt::lang_start::{{closure}}::h4a4f0688fcbbe2a8
   3: 0x55f1b1f1cf3a - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h02e5de65baee3e0a
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/ops/function.rs:280
                 - std::panicking::try::do_call::h928f62916d7b4bf7
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:492
                 - std::panicking::try::he5e8d9bda1fee0d9
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:456
                 - std::panic::catch_unwind::hbdacb0480f3c3a44
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panic.rs:137
                 - std::rt::lang_start_internal::{{closure}}::h9307a76627b54f4f
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/rt.rs:128
                 - std::panicking::try::do_call::h0baecbe86a821dd2
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:492
                 - std::panicking::try::h6c0869befc863f62
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:456
                 - std::panic::catch_unwind::h135074234245f66c
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panic.rs:137
                 - std::rt::lang_start_internal::h498f9556b87c8e5f
                at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/rt.rs:128
   4: 0x55f1b1ed4ed2 - main
   5: 0x7feeb3c40d0a - __libc_start_main
                at ./csu/../csu/libc-start.c:308
   6: 0x55f1b1ed1b3a - _start
   7:        0x0 - <unresolved>'''
rustc_version = '''
rustc 1.65.0-nightly (d394408fb 2022-08-07)
binary: rustc
commit-hash: d394408fb38c4de61f765a3ed5189d2731a1da91
commit-date: 2022-08-07
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6'''

Usage

use human_panic::setup_panic;

fn main() {
   setup_panic!();

   println!("A normal log message");
   panic!("OMG EVERYTHING IS ON FIRE!!!")
}

It only displays a human-friendly panic message in release mode:

$ cargo run --release

Installation

$ cargo add human-panic

License

MIT OR Apache-2.0