Expand description
§Chillpill
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:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.
Structs§
- Panic
Data - The payload and source code location of a panic.
- Panic
Location - The source code location of a panic.
Functions§
- catch
- Invokes a closure, capturing the cause, location, and backtrace of an unwinding panic if one
occurs, and suppressing the default panic output on
stderr. - catch_
force_ backtrace - Like
chillpill::catch, but always captures a backtrace. See its documentation for details. - catch_
never_ backtrace - Like
chillpill::catch, but never captures a backtrace. See its documentation for details.