#panic-hook #scoped #conveniently #infrastructure #scoped-panic-hook

scoped-panic-hook

Adds scoped, nestable, thread-local hooks for panics and some utilities for capturing and analyzing panics more conveniently

1 unstable release

Uses new Rust 2024

new 0.1.1 Apr 15, 2025
0.1.0 Apr 15, 2025

#1 in #conveniently

Download history 140/week @ 2025-04-11

140 downloads per month

MIT license

19KB
315 lines

Catching panics

Just use

let result = scoped_panic_hook::catch_panic(|| panic!("Oopsie!"));

if let Err(panic) = result {
    eprintln!("{}", panic.display_with_backtrace());
}

Any panic which happens inside closure supplied to catch_panic will be caught

Using manual panic hooks

In case you wnat to do something nontrivial, you can analyze panics manually

let mut counter = 0;

let _ = scoped_panic_hook::hook::catch_unwind_with_scoped_hook(
    |_| { counter += 1; scoped_panic_hook::hook::NextHook::PrevInstalledHook },
    || panic!("Oopsie!")
);

println!("Caught panics: {counter}");

Convenient panic catcher and scoped panic hooks infrastructure

This crate provides convenient API for obtaining panic info after unwinding

    let result = catch_panic(|| {
        panic!("Something bad happened!");
    });

    if let Err(panic) = result {
        eprintln!("{}", panic.display_with_backtrace());
    }

This will either produce normal result of closure or or conveniently gathered panic info, which includes panic location, message, raw payload and backtrace

Useful modules

  • hook - raw scoped hook APIs
  • panic - all types and functions relevant to capturing panics, including some advanced features

Development

Project uses cargo-xtask pattern. Run cargo xtask to see available commands

TODO

Test no-unwind cases

No runtime deps