pub fn set_hook(
    before_send: Option<Box<dyn Fn(Event) -> Event + Send + Sync + 'static>>,
    hook: Option<Box<dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static>>
)
Expand description

Panic handler to send an Event with the current stacktrace to Sentry.

before_send is a callback that is able to modify the Event before it is captures.

hook is a callback that is run after the Event is captured.

Notes

This will not work properly if used with panic = "abort" because Shutdown is never unwound. To fix this make sure you make the panic handler itself call shutdown.

Rust doesn’t allow panics inside of a panicking thread and reacts with an abort: if a custom transport or a before-send callback was registered that can panic, it might lead to any panic! being an abort instead.

Examples

fn main() -> Result<()> {
    // pass original panic handler provided by rust to retain it's functionality
    set_hook(None, Some(std::panic::take_hook()));
    // it can also be removed
    set_hook(None, None);
    // the `Event` sent by a panic can also be modified
    set_hook(
        Some(Box::new(|mut event| {
            // do something with the event and then return it
            event
        })),
        None,
    );

    let _shutdown = Options::new().init()?;

    panic!("application panicked")
}

If you are using panic = "abort" make sure to call shutdown inside the panic handler.

set_hook(None, Some(Box::new(|_| shutdown())));