rocketmq_error/
controller_error.rs1use std::io;
21
22use thiserror::Error;
23
24#[derive(Debug, Error)]
32pub enum ControllerError {
33 #[error("IO error: {0}")]
35 Io(#[from] io::Error),
36
37 #[error("Raft error: {0}")]
39 Raft(String),
40
41 #[error("Not leader, current leader is: {}", leader_id.map(|id| id.to_string()).unwrap_or_else(|| "unknown".to_string()))]
43 NotLeader { leader_id: Option<u64> },
44
45 #[error("Metadata not found: {key}")]
47 MetadataNotFound { key: String },
48
49 #[error("Invalid request: {0}")]
51 InvalidRequest(String),
52
53 #[error("Broker registration failed: {0}")]
55 BrokerRegistrationFailed(String),
56
57 #[error("Not initialized: {0}")]
59 NotInitialized(String),
60
61 #[error("Initialization failed")]
63 InitializationFailed,
64
65 #[error("Configuration error: {0}")]
67 ConfigError(String),
68
69 #[error("Serialization error: {0}")]
71 SerializationError(String),
72
73 #[error("Storage error: {0}")]
75 StorageError(String),
76
77 #[error("Network error: {0}")]
79 NetworkError(String),
80
81 #[error("Operation timeout after {timeout_ms}ms")]
83 Timeout { timeout_ms: u64 },
84
85 #[error("Internal error: {0}")]
87 Internal(String),
88
89 #[error("Controller is shutting down")]
91 Shutdown,
92}
93
94pub type ControllerResult<T> = std::result::Result<T, ControllerError>;
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 #[test]
102 fn test_controller_error() {
103 let err = ControllerError::Io(io::Error::other("test"));
104 assert_eq!(err.to_string(), "IO error: test");
105
106 let err = ControllerError::Raft("raft error".to_string());
107 assert_eq!(err.to_string(), "Raft error: raft error");
108
109 let err = ControllerError::NotLeader { leader_id: Some(1) };
110 assert_eq!(err.to_string(), "Not leader, current leader is: 1");
111
112 let err = ControllerError::NotLeader { leader_id: None };
113 assert_eq!(err.to_string(), "Not leader, current leader is: unknown");
114
115 let err = ControllerError::MetadataNotFound {
116 key: "broker-a".to_string(),
117 };
118 assert_eq!(err.to_string(), "Metadata not found: broker-a");
119
120 let err = ControllerError::InvalidRequest("bad request".to_string());
121 assert_eq!(err.to_string(), "Invalid request: bad request");
122
123 let err = ControllerError::BrokerRegistrationFailed("failed".to_string());
124 assert_eq!(err.to_string(), "Broker registration failed: failed");
125
126 let err = ControllerError::NotInitialized("init first".to_string());
127 assert_eq!(err.to_string(), "Not initialized: init first");
128
129 let err = ControllerError::InitializationFailed;
130 assert_eq!(err.to_string(), "Initialization failed");
131
132 let err = ControllerError::ConfigError("invalid config".to_string());
133 assert_eq!(err.to_string(), "Configuration error: invalid config");
134
135 let err = ControllerError::SerializationError("serde error".to_string());
136 assert_eq!(err.to_string(), "Serialization error: serde error");
137
138 let err = ControllerError::StorageError("disk full".to_string());
139 assert_eq!(err.to_string(), "Storage error: disk full");
140
141 let err = ControllerError::NetworkError("disconnected".to_string());
142 assert_eq!(err.to_string(), "Network error: disconnected");
143
144 let err = ControllerError::Timeout { timeout_ms: 5000 };
145 assert_eq!(err.to_string(), "Operation timeout after 5000ms");
146
147 let err = ControllerError::Internal("panic".to_string());
148 assert_eq!(err.to_string(), "Internal error: panic");
149
150 let err = ControllerError::Shutdown;
151 assert_eq!(err.to_string(), "Controller is shutting down");
152 }
153}