tin_redis_conn/
error.rs

1use std::fmt;
2
3/// Redis 连接相关的错误类型
4#[derive(Debug)]
5pub enum ConnectionError {
6    /// Redis 客户端创建失败
7    ClientCreation(redis::RedisError),
8    /// 连接池创建失败
9    PoolCreation(String),
10    /// 连接获取失败
11    ConnectionAcquisition(redis::RedisError),
12    /// 连接管理器错误
13    ConnectionManager(redis::RedisError),
14    /// 配置错误
15    Configuration(String),
16    /// 连接超时
17    Timeout,
18    /// 网络错误
19    Network(String),
20    /// 序列化错误
21    Serialization(String),
22    /// 反序列化错误
23    Deserialization(String),
24}
25
26impl fmt::Display for ConnectionError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            ConnectionError::ClientCreation(e) => write!(f, "Failed to create Redis client: {e}"),
30            ConnectionError::PoolCreation(msg) => {
31                write!(f, "Failed to create connection pool: {msg}")
32            }
33            ConnectionError::ConnectionAcquisition(e) => {
34                write!(f, "Failed to acquire connection: {e}")
35            }
36            ConnectionError::ConnectionManager(e) => write!(f, "Connection manager error: {e}"),
37            ConnectionError::Configuration(msg) => write!(f, "Configuration error: {msg}"),
38            ConnectionError::Timeout => write!(f, "Connection timeout"),
39            ConnectionError::Network(msg) => write!(f, "Network error: {msg}"),
40            ConnectionError::Serialization(msg) => write!(f, "Serialization error: {msg}"),
41            ConnectionError::Deserialization(msg) => write!(f, "Deserialization error: {msg}"),
42        }
43    }
44}
45
46impl std::error::Error for ConnectionError {}
47
48impl From<redis::RedisError> for ConnectionError {
49    fn from(err: redis::RedisError) -> Self {
50        ConnectionError::ClientCreation(err)
51    }
52}
53
54/// 结果类型别名
55pub type Result<T> = std::result::Result<T, ConnectionError>;