1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, ResponseError>;
7
8#[derive(Error, Debug)]
10pub enum ResponseError {
11 #[error("Meta-learning error: {0}")]
12 MetaLearning(String),
13
14 #[error("Mitigation failed: {0}")]
15 MitigationFailed(String),
16
17 #[error("Strategy not found: {0}")]
18 StrategyNotFound(String),
19
20 #[error("Rollback failed: {0}")]
21 RollbackFailed(String),
22
23 #[error("Audit logging error: {0}")]
24 AuditError(String),
25
26 #[error("Invalid configuration: {0}")]
27 InvalidConfiguration(String),
28
29 #[error("Resource unavailable: {0}")]
30 ResourceUnavailable(String),
31
32 #[error("Timeout during {operation}: {details}")]
33 Timeout {
34 operation: String,
35 details: String,
36 },
37
38 #[error("Strange-loop error: {0}")]
39 StrangeLoopError(#[from] midstreamer_strange_loop::StrangeLoopError),
40
41 #[error("AIMDS core error: {0}")]
42 CoreError(#[from] aimds_core::AimdsError),
43
44 #[error("IO error: {0}")]
45 Io(#[from] std::io::Error),
46
47 #[error("Serialization error: {0}")]
48 Serialization(#[from] serde_json::Error),
49
50 #[error("Other error: {0}")]
51 Other(#[from] anyhow::Error),
52}
53
54impl ResponseError {
55 pub fn is_retryable(&self) -> bool {
57 matches!(
58 self,
59 ResponseError::Timeout { .. }
60 | ResponseError::ResourceUnavailable(_)
61 )
62 }
63
64 pub fn severity(&self) -> ErrorSeverity {
66 match self {
67 ResponseError::MitigationFailed(_) => ErrorSeverity::Critical,
68 ResponseError::RollbackFailed(_) => ErrorSeverity::Critical,
69 ResponseError::MetaLearning(_) => ErrorSeverity::Warning,
70 ResponseError::Timeout { .. } => ErrorSeverity::Warning,
71 _ => ErrorSeverity::Error,
72 }
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
78pub enum ErrorSeverity {
79 Critical,
80 Error,
81 Warning,
82 Info,
83}