add error_and_panic helper for logerr

This commit is contained in:
emilis 2024-03-03 14:51:07 +00:00
parent 3e6a944df8
commit 21adbec2ce
1 changed files with 14 additions and 8 deletions

View File

@ -2,6 +2,14 @@ use std::{error::Error, fmt::Debug};
use log::{debug, error};
// So we still get a useful panic when logs are filtered
macro_rules! error_and_panic {
($($arg:tt)*) => {
log::error!($($arg)*);
panic!($($arg)*);
};
}
pub trait UnwrapLog {
type Target;
@ -35,8 +43,7 @@ where
match self {
Ok(target) => target,
Err(err) => {
error!("called `Result::unwrap_log()` on an `Err` value: {err}");
panic!("called `Result::unwrap_log()` on an `Err` value: {err}");
error_and_panic!("called `Result::unwrap_log()` on an `Err` value: {err}");
}
}
}
@ -45,8 +52,9 @@ where
match self {
Ok(target) => target,
Err(err) => {
error!("[{expect}] called `Result::expect_log()` on an `Err` value: {err}");
panic!("[{expect}] called `Result::expect_log()` on an `Err` value: {err}");
error_and_panic!(
"[{expect}] called `Result::expect_log()` on an `Err` value: {err}"
);
}
}
}
@ -76,8 +84,7 @@ where
match self {
Some(target) => target,
None => {
error!("called `Option::unwrap_log()` on a `None` value");
panic!("called `Option::unwrap_log()` on a `None` value");
error_and_panic!("called `Option::unwrap_log()` on a `None` value");
}
}
}
@ -86,8 +93,7 @@ where
match self {
Some(target) => target,
None => {
error!("[{expect}] called `Option::expect_log()` on an `None` value");
panic!("[{expect}] called `Option::expect_log()` on an `None` value");
error_and_panic!("[{expect}] called `Option::expect_log()` on an `None` value");
}
}
}