rocketmq_controller/
error.rs1use std::io;
19
20use thiserror::Error;
21
22#[derive(Debug, Error)]
24pub enum ControllerError {
25 #[error("IO error: {0}")]
27 Io(#[from] io::Error),
28
29 #[error("Raft error: {0}")]
31 Raft(String),
32
33 #[error("Not leader, current leader is: {}", leader_id.map(|id| id.to_string()).unwrap_or_else(|| "unknown".to_string()))]
35 NotLeader { leader_id: Option<u64> },
36
37 #[error("Metadata not found: {key}")]
39 MetadataNotFound { key: String },
40
41 #[error("Invalid request: {0}")]
43 InvalidRequest(String),
44
45 #[error("Broker registration failed: {0}")]
47 BrokerRegistrationFailed(String),
48
49 #[error("Configuration error: {0}")]
51 ConfigError(String),
52
53 #[error("Serialization error: {0}")]
55 SerializationError(String),
56
57 #[error("Storage error: {0}")]
59 StorageError(String),
60
61 #[error("Network error: {0}")]
63 NetworkError(String),
64
65 #[error("Operation timeout after {timeout_ms}ms")]
67 Timeout { timeout_ms: u64 },
68
69 #[error("Internal error: {0}")]
71 Internal(String),
72
73 #[error("Controller is shutting down")]
75 Shutdown,
76}
77
78impl From<serde_json::Error> for ControllerError {
79 fn from(e: serde_json::Error) -> Self {
80 ControllerError::SerializationError(e.to_string())
81 }
82}
83
84impl From<bincode::Error> for ControllerError {
85 fn from(e: bincode::Error) -> Self {
86 ControllerError::SerializationError(e.to_string())
87 }
88}
89
90impl From<raft::Error> for ControllerError {
91 fn from(e: raft::Error) -> Self {
92 ControllerError::Raft(format!("{:?}", e))
93 }
94}
95
96pub type Result<T> = std::result::Result<T, ControllerError>;
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_error_display() {
105 let err = ControllerError::NotLeader { leader_id: Some(1) };
106 assert!(err.to_string().contains("Not leader"));
107 }
108
109 #[test]
110 fn test_error_conversion() {
111 let io_err = io::Error::other("test");
112 let controller_err: ControllerError = io_err.into();
113 assert!(matches!(controller_err, ControllerError::Io(_)));
114 }
115}