Skip to main content

signer_daemon/
error.rs

1use thiserror::Error;
2
3// 复用 signer-core 中的错误类型与统一结果类型
4pub use signer_core::{Result, SignerError};
5
6/// signer-daemon 特有的错误类型扩展
7#[derive(Debug, Error)]
8pub enum DaemonError {
9    /// 数据库操作错误
10    #[error("数据库操作失败: {0}")]
11    Database(#[from] sea_orm::DbErr),
12
13    /// 网络请求错误
14    #[error("网络请求失败: {0}")]
15    Network(#[from] reqwest::Error),
16
17    /// IO 操作错误
18    #[error("IO 操作失败: {0}")]
19    Io(#[from] std::io::Error),
20
21    /// JSON 序列化错误
22    #[error("JSON 序列化失败: {0}")]
23    Json(#[from] serde_json::Error),
24
25    /// Base64 解码错误
26    #[error("Base64 解码失败: {0}")]
27    Base64(#[from] base64::DecodeError),
28
29    /// UTF8 转换错误
30    #[error("UTF8 转换失败: {0}")]
31    Utf8(#[from] std::string::FromUtf8Error),
32
33    /// 事件源客户端错误
34    #[error("事件源客户端错误: {0}")]
35    EventSource(String),
36
37    /// 通用 Signer 错误
38    #[error(transparent)]
39    Signer(#[from] SignerError),
40}
41
42/// 统一的结果类型,使用 DaemonError
43pub type DaemonResult<T> = std::result::Result<T, DaemonError>;
44
45/// 为 SignerError 添加从 DaemonError 的转换
46impl From<DaemonError> for SignerError {
47    fn from(err: DaemonError) -> Self {
48        match err {
49            DaemonError::Signer(e) => e,
50            _ => SignerError::Msg(err.to_string()),
51        }
52    }
53}
54
55/// 为 eventsource_client::Error 添加转换实现
56impl From<eventsource_client::Error> for DaemonError {
57    fn from(err: eventsource_client::Error) -> Self {
58        DaemonError::EventSource(err.to_string())
59    }
60}