2 个稳定版本

1.1.5 2023 年 6 月 4 日

#429命令行界面

每月 25 次下载

MIT/Apache

25KB
272

not-so-human-panic

crates.io version build status downloads docs.rs docs

为人类设计的恐慌消息。通过调用 std::panic::set_hook 来处理恐慌,使错误对人类友好。

为什么?

当你构建 CLI 时,外观非常重要。虽然 Rust 在安全性方面做得很好,但访问向量中的错误索引或断言失败并不罕见。

当错误最终发生时,你可能想知道关于它的信息。所以我们不仅可以提供命令行上的错误消息,还可以为人们提交报告创建一个行动号召。

这应该使人们能够参与沟通,降低人们可能感到沮丧的可能性。并且使确定可能引起错误的原因变得更容易。

默认输出

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

非人类恐慌输出

Well, this is embarrassing.

not-so-human-panic had a problem and crashed. It seems that the problem has to do with the following:
OMG EVERYTHING IS ON FIRE!!! 

If you'd like, you can help us diagnose the problem! Please feel free to send us a crash report using the instructions below.

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 "single-panic-test Crash Report" and include the report as an attachment.

- Authors: Yoshua Wuyts <[email protected]>
- Homepage: https://github.com/onkoe/not-so-human-panic

We take privacy very seriously - we don't perform any automated error collection. In order to improve the software, we rely on users like you to submit reports.

Thank you kindly!

not-so-human-panic 生成的错误转储文件包含以下字段。

name = 'single-panic-test'
operating_system = 'unix:Unknown'
crate_version = '0.1.0'
explanation = '''
Cause: OMG EVERYTHING IS ON FIRE!!!. Panic occurred in file 'tests/single-panic/src/main.rs' at line 8
'''
method = 'Panic'
backtrace = '''
stack backtrace:
   0:     0x55fa0ed4c1b4 - backtrace::backtrace::libunwind::trace::h69e50feca54bfb84
                        at /home/spacekookie/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.6/src/backtrace/libunwind.rs:53
                         - backtrace::backtrace::trace::h42967341e0b01ccc
                        at /home/spacekookie/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.6/src/backtrace/mod.rs:42
    
    # ...

   8:     0x55fa0ebaac8d - single_panic_test::main::h56a3d326bcecfc36
                        at tests/single-panic/src/main.rs:8
   9:     0x55fa0ebaae91 - std::rt::lang_start::{{closure}}::h09d28d8540038bf8
                        at /checkout/src/libstd/rt.rs:74
  10:     0x55fa0ed732f7 - std::rt::lang_start_internal::{{closure}}::h2e4baf0a27c956a3
                        at libstd/rt.rs:59
                         - std::panicking::try::do_call::h73f98ed0647c7274
                        at libstd/panicking.rs:305
  11:     0x55fa0ed8551e - __rust_maybe_catch_panic
                        at libpanic_unwind/lib.rs:101
  12:     0x55fa0ed6f7f5 - std::panicking::try::h18fbb145180d4cd9
                        at libstd/panicking.rs:284
                         - std::panic::catch_unwind::hc4b6a212a30b4bc5
                        at libstd/panic.rs:361
                         - std::rt::lang_start_internal::h8b001b4244930d51
                        at libstd/rt.rs:58
  13:     0x55fa0ebaae71 - std::rt::lang_start::h1b1de624209f414a
                        at /checkout/src/libstd/rt.rs:74
  14:     0x55fa0ebaacbd - main
  15:     0x7f9946132f29 - __libc_start_main
  16:     0x55fa0eba9b79 - _start
  17:                0x0 - <unknown>'''

用法

use not_so_human_panic::setup_panic;

fn main() {
   setup_panic!();

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

仅在发布模式下显示人类友好的恐慌消息

cargo run --release

安装

cargo add not-so-human-panic

许可证

MIT OR Apache-2.0

依赖关系

~3–13MB
~149K SLoC