Skip to main content

redis_sentinel_pool/
error.rs

1//! 错误类型定义。
2//!
3//! 本模块统一封装 Sentinel 连接池可能产生的所有错误,便于上层使用 `?`
4//! 操作符或 `match` 进行分支处理。
5
6use thiserror::Error;
7
8/// 连接池操作的统一错误类型。
9#[derive(Debug, Error)]
10pub enum SentinelPoolError {
11    /// 底层 redis-rs 抛出的错误(含 Sentinel 解析失败、命令执行失败等)。
12    #[error("redis error: {0}")]
13    Redis(#[from] redis::RedisError),
14
15    /// bb8 连接池本身的错误(例如等待超时、池已关闭等)。
16    #[error("connection pool error: {0}")]
17    Pool(String),
18
19    /// 配置参数非法。
20    #[error("invalid configuration: {0}")]
21    Config(String),
22
23    /// 在多次重试后命令仍然失败。
24    #[error("operation failed after {attempts} attempts: {source}")]
25    RetryExhausted {
26        /// 已经尝试的次数。
27        attempts: u32,
28        /// 最后一次失败的原因。
29        #[source]
30        source: redis::RedisError,
31    },
32
33    /// 后台 watcher 因为不可恢复的错误已经退出。
34    #[error("sentinel watcher stopped: {0}")]
35    WatcherStopped(String),
36}
37
38/// 本 crate 的统一 `Result` 别名。
39pub type Result<T> = std::result::Result<T, SentinelPoolError>;
40
41impl<E> From<bb8::RunError<E>> for SentinelPoolError
42where
43    E: Into<SentinelPoolError> + std::fmt::Display,
44{
45    fn from(value: bb8::RunError<E>) -> Self {
46        match value {
47            bb8::RunError::User(e) => e.into(),
48            bb8::RunError::TimedOut => SentinelPoolError::Pool("timed out waiting for connection".into()),
49        }
50    }
51}