Skip to main content

quant_governor/
error.rs

1//! Error types for quant-governor.
2
3use thiserror::Error;
4
5/// Errors that can occur during governance evaluation.
6#[derive(Debug, Error)]
7pub enum GovernorError {
8    /// Invalid request parameters
9    #[error("Invalid request: {0}")]
10    InvalidRequest(String),
11
12    /// Policy evaluation failed
13    #[error("Policy evaluation failed: {0}")]
14    EvaluationFailed(String),
15
16    /// Invalid degradation threshold
17    #[error("Invalid degradation threshold: {0}")]
18    InvalidThreshold(String),
19
20    /// Content type not supported
21    #[error("Unsupported content type: {0}")]
22    UnsupportedContentType(String),
23
24    /// Internal error
25    #[error("Internal error: {0}")]
26    Internal(String),
27}
28
29impl GovernorError {
30    /// Returns true if this is a recoverable error.
31    pub fn is_recoverable(&self) -> bool {
32        matches!(
33            self,
34            GovernorError::InvalidThreshold(_) | GovernorError::UnsupportedContentType(_)
35        )
36    }
37
38    /// Returns true if this indicates a system configuration issue.
39    pub fn is_configuration_error(&self) -> bool {
40        matches!(
41            self,
42            GovernorError::InvalidThreshold(_) | GovernorError::Internal(_)
43        )
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn error_recoverability() {
53        let err = GovernorError::InvalidThreshold("too high".to_string());
54        assert!(err.is_recoverable());
55
56        let err = GovernorError::Internal("bug".to_string());
57        assert!(!err.is_recoverable());
58    }
59
60    #[test]
61    fn error_display() {
62        let err = GovernorError::InvalidRequest("bad value".to_string());
63        assert_eq!(err.to_string(), "Invalid request: bad value");
64    }
65}