use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum GuardError {
Timeout,
DeadlineViolated,
Panicked(String),
Internal(String),
Dependency(String),
}
impl GuardError {
pub fn panicked(message: impl Into<String>) -> Self {
GuardError::Panicked(message.into())
}
pub fn internal(message: impl Into<String>) -> Self {
GuardError::Internal(message.into())
}
pub fn dependency(message: impl Into<String>) -> Self {
GuardError::Dependency(message.into())
}
pub fn is_timeout(&self) -> bool {
matches!(self, GuardError::Timeout | GuardError::DeadlineViolated)
}
pub fn is_panic(&self) -> bool {
matches!(self, GuardError::Panicked(_))
}
pub fn kind(&self) -> &'static str {
match self {
GuardError::Timeout => "timeout",
GuardError::DeadlineViolated => "deadline_violated",
GuardError::Panicked(_) => "panicked",
GuardError::Internal(_) => "internal",
GuardError::Dependency(_) => "dependency",
}
}
pub fn message(&self) -> Option<&str> {
match self {
GuardError::Panicked(message)
| GuardError::Internal(message)
| GuardError::Dependency(message) => Some(message),
GuardError::Timeout | GuardError::DeadlineViolated => None,
}
}
}
impl fmt::Display for GuardError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GuardError::Timeout => f.write_str("guard timed out"),
GuardError::DeadlineViolated => {
f.write_str("strict guard exceeded its deadline without yielding")
}
GuardError::Panicked(message) => write!(f, "guard panicked: {message}"),
GuardError::Internal(message) => write!(f, "guard failed internally: {message}"),
GuardError::Dependency(message) => write!(f, "guard dependency failed: {message}"),
}
}
}
impl std::error::Error for GuardError {}
#[cfg(test)]
mod tests {
use super::*;
fn all() -> Vec<GuardError> {
vec![
GuardError::Timeout,
GuardError::DeadlineViolated,
GuardError::Panicked("boom".to_owned()),
GuardError::Internal("bad rule".to_owned()),
GuardError::Dependency("classifier unreachable".to_owned()),
]
}
#[test]
fn kinds_are_unique_and_payload_free() {
let mut kinds: Vec<_> = all().iter().map(GuardError::kind).collect();
let count = kinds.len();
kinds.sort_unstable();
kinds.dedup();
assert_eq!(kinds.len(), count, "two variants share a kind label");
for error in all() {
assert!(
!error.kind().contains(' '),
"kind labels must be usable as metric dimensions"
);
}
}
#[test]
fn timeout_predicates_cover_both_time_variants() {
assert!(GuardError::Timeout.is_timeout());
assert!(GuardError::DeadlineViolated.is_timeout());
assert!(!GuardError::internal("x").is_timeout());
assert!(!GuardError::dependency("x").is_timeout());
}
#[test]
fn only_panicked_is_a_panic() {
assert!(GuardError::Panicked("boom".to_owned()).is_panic());
assert!(!GuardError::internal("boom").is_panic());
}
#[test]
fn messages_are_carried_by_the_variants_that_have_them() {
assert_eq!(GuardError::internal("bad rule").message(), Some("bad rule"));
assert_eq!(GuardError::dependency("down").message(), Some("down"));
assert_eq!(
GuardError::Panicked("boom".to_owned()).message(),
Some("boom")
);
assert_eq!(GuardError::Timeout.message(), None);
assert_eq!(GuardError::DeadlineViolated.message(), None);
}
#[test]
fn display_includes_the_message_for_diagnostics() {
assert_eq!(
GuardError::Panicked("index out of bounds".to_owned()).to_string(),
"guard panicked: index out of bounds"
);
assert_eq!(GuardError::Timeout.to_string(), "guard timed out");
}
#[test]
fn is_a_std_error() {
fn assert_error<E: std::error::Error>(_: &E) {}
for error in all() {
assert_error(&error);
}
}
}