nsq_async_rs/
error.rs

1use std::io;
2use thiserror::Error;
3use tokio::time::error::Elapsed;
4
5use crate::protocol::ProtocolError;
6
7/// NSQ客户端库错误类型
8#[derive(Debug, Error)]
9pub enum Error {
10    /// IO错误
11    #[error("IO错误: {0}")]
12    Io(#[from] io::Error),
13
14    /// 序列化错误
15    #[error("序列化错误: {0}")]
16    Serialize(#[from] serde_json::Error),
17
18    /// 连接错误
19    #[error("连接错误: {0}")]
20    Connection(String),
21
22    /// 协议错误
23    #[error("协议错误: {0}")]
24    Protocol(#[from] ProtocolError),
25
26    /// 超时错误
27    #[error("操作超时")]
28    Timeout(#[from] Elapsed),
29
30    /// 认证错误
31    #[error("认证失败: {0}")]
32    Auth(String),
33
34    /// 消息处理错误
35    #[error("消息处理错误: {0}")]
36    MessageHandling(String),
37
38    /// 配置错误
39    #[error("配置错误: {0}")]
40    Config(String),
41
42    /// HTTP错误
43    #[error("HTTP错误: {0}")]
44    Http(#[from] reqwest::Error),
45
46    /// 其他错误
47    #[error("其他错误: {0}")]
48    Other(String),
49}
50
51impl From<backoff::Error<Error>> for Error {
52    fn from(err: backoff::Error<Error>) -> Self {
53        match err {
54            backoff::Error::Permanent(e) => e,
55            backoff::Error::Transient { err, .. } => err,
56        }
57    }
58}
59
60/// Result类型别名,用于NSQ客户端库
61pub type Result<T> = std::result::Result<T, Error>;