Skip to main content

baichun_framework_rms/
error.rs

1//! 错误处理模块
2//!
3//! 本模块定义了 RMS 系统中使用的所有错误类型和错误处理工具。
4
5use rdkafka::error::KafkaError;
6use rdkafka::types::RDKafkaErrorCode;
7use std::fmt;
8
9/// RMS 错误类型
10///
11/// 这个枚举定义了 RMS 系统中可能出现的所有错误类型。
12/// 每种错误类型都包含详细的错误信息,以帮助定位和解决问题。
13#[derive(Debug, thiserror::Error)]
14pub enum Error {
15    /// 消息通道相关错误
16    ///
17    /// 当消息的发送、接收或通道操作出现问题时返回此错误。
18    #[error("Channel error: {0}")]
19    Channel(String),
20
21    /// 消息转换错误
22    ///
23    /// 当消息在不同格式之间转换失败时返回此错误。
24    #[error("Conversion error: {0}")]
25    Conversion(String),
26
27    /// 消息验证错误
28    ///
29    /// 当消息不满足验证规则时返回此错误。
30    #[error("Validation error: {0}")]
31    Validation(String),
32
33    /// 配置错误
34    ///
35    /// 当系统配置无效或缺失时返回此错误。
36    #[error("Configuration error: {0}")]
37    Config(String),
38
39    /// 连接错误
40    ///
41    /// 当与消息中间件的连接出现问题时返回此错误。
42    #[error("Connection error: {0}")]
43    Connection(String),
44
45    /// 超时错误
46    ///
47    /// 当操作超过指定时间限制时返回此错误。
48    #[error("Timeout error: {0}")]
49    Timeout(String),
50
51    /// 其他错误
52    ///
53    /// 不属于上述类别的其他错误。
54    #[error("Other error: {0}")]
55    Other(String),
56}
57
58impl Error {
59    /// 创建通道错误
60    ///
61    /// # 参数
62    ///
63    /// * `err` - 实现了 `Display` trait 的错误信息
64    pub fn channel<T: fmt::Display>(err: T) -> Self {
65        Self::Channel(err.to_string())
66    }
67
68    /// 创建转换错误
69    pub fn conversion<T: fmt::Display>(err: T) -> Self {
70        Self::Conversion(err.to_string())
71    }
72
73    /// 创建验证错误
74    pub fn validation<T: fmt::Display>(err: T) -> Self {
75        Self::Validation(err.to_string())
76    }
77
78    /// 创建配置错误
79    pub fn config<T: fmt::Display>(err: T) -> Self {
80        Self::Config(err.to_string())
81    }
82
83    /// 创建连接错误
84    pub fn connection<T: fmt::Display>(err: T) -> Self {
85        Self::Connection(err.to_string())
86    }
87
88    /// 创建超时错误
89    pub fn timeout<T: fmt::Display>(err: T) -> Self {
90        Self::Timeout(err.to_string())
91    }
92
93    /// 创建其他错误
94    pub fn other<T: fmt::Display>(err: T) -> Self {
95        Self::Other(err.to_string())
96    }
97
98    /// 判断错误是否为超时错误
99    pub fn is_timeout(&self) -> bool {
100        matches!(self, Self::Timeout(_))
101    }
102
103    /// 判断错误是否为连接错误
104    pub fn is_connection_error(&self) -> bool {
105        matches!(self, Self::Connection(_))
106    }
107}
108
109/// RMS 结果类型别名
110pub type Result<T> = std::result::Result<T, Error>;
111
112// 实现从各种错误类型到 RMS Error 的转换
113impl From<KafkaError> for Error {
114    fn from(err: KafkaError) -> Self {
115        match err {
116            KafkaError::Global(code) => match code {
117                RDKafkaErrorCode::MessageTimedOut => Self::Timeout(err.to_string()),
118                RDKafkaErrorCode::BrokerTransportFailure => Self::Connection(err.to_string()),
119                RDKafkaErrorCode::AllBrokersDown => Self::Connection(err.to_string()),
120                _ => Self::Other(err.to_string()),
121            },
122            _ => Self::Other(err.to_string()),
123        }
124    }
125}
126
127impl From<std::io::Error> for Error {
128    fn from(err: std::io::Error) -> Self {
129        match err.kind() {
130            std::io::ErrorKind::TimedOut => Self::Timeout(err.to_string()),
131            std::io::ErrorKind::ConnectionRefused
132            | std::io::ErrorKind::ConnectionReset
133            | std::io::ErrorKind::ConnectionAborted => Self::Connection(err.to_string()),
134            _ => Self::Other(err.to_string()),
135        }
136    }
137}
138
139impl From<serde_json::Error> for Error {
140    fn from(err: serde_json::Error) -> Self {
141        Self::Conversion(err.to_string())
142    }
143}
144
145impl From<tokio::sync::broadcast::error::SendError<()>> for Error {
146    fn from(err: tokio::sync::broadcast::error::SendError<()>) -> Self {
147        Self::Channel(err.to_string())
148    }
149}