2021-05-13 17:25:45 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::{panic, thread};
|
|
|
|
|
|
|
|
use backtrace::Backtrace;
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
pub fn error_handler(In(result): In<Result<(), Box<dyn Error>>>) {
|
|
|
|
if let Err(e) = result {
|
|
|
|
error!("{}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_panic_handler() {
|
|
|
|
panic::set_hook(Box::new(|info| {
|
|
|
|
let backtrace = Backtrace::default();
|
|
|
|
let thread = thread::current();
|
|
|
|
let thread = thread.name().unwrap_or("<unnamed>");
|
|
|
|
let msg = match info.payload().downcast_ref::<&'static str>() {
|
|
|
|
Some(s) => *s,
|
|
|
|
None => match info.payload().downcast_ref::<String>() {
|
|
|
|
Some(s) => &**s,
|
|
|
|
None => "Box<Any>",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
match info.location() {
|
|
|
|
Some(location) => {
|
|
|
|
error!(
|
|
|
|
target: "panic", "thread '{}' panicked at '{}': {}:{}{:?}",
|
|
|
|
thread,
|
|
|
|
msg,
|
|
|
|
location.file(),
|
|
|
|
location.line(),
|
|
|
|
backtrace
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => error!(
|
|
|
|
target: "panic",
|
|
|
|
"thread '{}' panicked at '{}'{:?}",
|
|
|
|
thread,
|
|
|
|
msg,
|
|
|
|
backtrace
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:44:55 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct ErrorConfig {
|
|
|
|
pub sentry_dsn: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-05-13 17:25:45 +00:00
|
|
|
pub struct ErrorPlugin;
|
|
|
|
|
|
|
|
impl Plugin for ErrorPlugin {
|
2021-08-30 16:44:55 +00:00
|
|
|
fn build(&self, app: &mut AppBuilder) {
|
2021-05-13 17:25:45 +00:00
|
|
|
init_panic_handler();
|
2021-08-30 16:44:55 +00:00
|
|
|
if let Some(config) = app.world().get_resource::<ErrorConfig>() {
|
|
|
|
if let Some(dsn) = &config.sentry_dsn {
|
|
|
|
let guard = sentry::init(dsn.clone());
|
|
|
|
app.insert_resource(guard);
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 17:25:45 +00:00
|
|
|
}
|
|
|
|
}
|