rocketmq_controller/
error.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use std::io;
19
20use thiserror::Error;
21
22/// Controller module error types
23#[derive(Debug, Error)]
24pub enum ControllerError {
25    /// IO errors
26    #[error("IO error: {0}")]
27    Io(#[from] io::Error),
28
29    /// Raft consensus errors
30    #[error("Raft error: {0}")]
31    Raft(String),
32
33    /// Not the leader error
34    #[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    /// Metadata not found
38    #[error("Metadata not found: {key}")]
39    MetadataNotFound { key: String },
40
41    /// Invalid request
42    #[error("Invalid request: {0}")]
43    InvalidRequest(String),
44
45    /// Broker registration error
46    #[error("Broker registration failed: {0}")]
47    BrokerRegistrationFailed(String),
48
49    /// Configuration error
50    #[error("Configuration error: {0}")]
51    ConfigError(String),
52
53    /// Serialization error
54    #[error("Serialization error: {0}")]
55    SerializationError(String),
56
57    /// Storage error
58    #[error("Storage error: {0}")]
59    StorageError(String),
60
61    /// Network error
62    #[error("Network error: {0}")]
63    NetworkError(String),
64
65    /// Timeout error
66    #[error("Operation timeout after {timeout_ms}ms")]
67    Timeout { timeout_ms: u64 },
68
69    /// Internal error
70    #[error("Internal error: {0}")]
71    Internal(String),
72
73    /// Shutdown error
74    #[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
96/// Result type alias for Controller operations
97pub 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}