use thiserror::Error;
#[derive(Debug, Error)]
pub enum GovernorError {
#[error("Invalid request: {0}")]
InvalidRequest(String),
#[error("Policy evaluation failed: {0}")]
EvaluationFailed(String),
#[error("Invalid degradation threshold: {0}")]
InvalidThreshold(String),
#[error("Unsupported content type: {0}")]
UnsupportedContentType(String),
#[error("Internal error: {0}")]
Internal(String),
}
impl GovernorError {
pub fn is_recoverable(&self) -> bool {
matches!(
self,
GovernorError::InvalidThreshold(_) | GovernorError::UnsupportedContentType(_)
)
}
pub fn is_configuration_error(&self) -> bool {
matches!(
self,
GovernorError::InvalidThreshold(_) | GovernorError::Internal(_)
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_recoverability() {
let err = GovernorError::InvalidThreshold("too high".to_string());
assert!(err.is_recoverable());
let err = GovernorError::Internal("bug".to_string());
assert!(!err.is_recoverable());
}
#[test]
fn error_display() {
let err = GovernorError::InvalidRequest("bad value".to_string());
assert_eq!(err.to_string(), "Invalid request: bad value");
}
}