use std::panic;
use api::protocol::{Event, Exception, Level};
use backtrace_support::current_stacktrace;
use scope::{scope_panic_safe, with_client_and_scope};
pub fn message_from_panic_info<'a>(info: &'a panic::PanicInfo) -> &'a str {
match info.payload().downcast_ref::<&'static str>() {
Some(s) => *s,
None => match info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "Box<Any>",
},
}
}
pub fn event_from_panic_info(info: &panic::PanicInfo) -> Event<'static> {
let msg = message_from_panic_info(info);
Event {
exceptions: vec![
Exception {
ty: "panic".into(),
value: Some(msg.to_string()),
stacktrace: current_stacktrace(),
..Default::default()
},
],
level: Level::Fatal,
..Default::default()
}
}
pub fn panic_handler(info: &panic::PanicInfo) {
if scope_panic_safe() {
with_client_and_scope(|client, scope| {
client.capture_event(event_from_panic_info(info), Some(scope));
});
}
}
pub fn register_panic_handler() {
let next = panic::take_hook();
panic::set_hook(Box::new(move |info| {
panic_handler(info);
next(info);
}));
}