#panic #catch-unwind #error-handling #catch

chillpill

A more powerful (and more restrictive) std::panic::catch_unwind

1 unstable release

Uses new Rust 2024

new 0.1.0 May 2, 2025

#2155 in Rust patterns

MIT/Apache

37KB
365 lines

Chillpill

GitHub crates.io docs.rs License

A more powerful (and more restrictive) std::panic::catch_unwind.

chillpill::catch is able to suppress the default panic message printed to stderr and return the source code location (file, line, and column) of the panic, at the cost of requiring that no other code modifies the global panic hook during its execution.

See the chillpill::catch documentation for a full list of differences from std::panic::catch_unwind, and more information on the panic hook restriction.

Example Usage

chillpill::catch is a drop-in replacement for std::panic::catch_unwind:

use chillpill::{PanicData, PanicLocation};

fn main() {
    // The API of `chillpill::catch` is the same as `std::panic::catch_unwind`
    //
    // The important differences are outlined in the documentation for `catch`,
    // but this example demonstrates that panic messages are suppressed, and the
    // location of the panic (file, line, and column) are available at runtime.
    let panic_result: Result<(), PanicData> = chillpill::catch(|| {
        // You won't see this message on stderr, chillpill prevents panic output
        panic!("Uh oh, I'm freaking out!!!");
    });

    let panic_data = panic_result.unwrap_err();
    assert_eq!(
        panic_data.payload_as_string(),
        Some("Uh oh, I'm freaking out!!!")
    );

    let PanicLocation { file, line, col } = panic_data.location.unwrap();
    println!("The panic occurred in {file} on line {line}, column {col}.");
}
$ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/x86_64-unknown-linux-gnu/debug/example`
The panic occurred in src/main.rs on line 11, column 9.

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps