clamber_core/
error.rs

1use thiserror::Error;
2
3/// Clamber Core 库的统一错误类型
4#[derive(Error, Debug)]
5pub enum ClamberError {
6    /// 日志相关错误
7    #[error("日志系统错误: {message}")]
8    LoggingError { message: String },
9
10    /// 目录创建错误
11    #[error("创建目录失败: {path}")]
12    DirectoryCreationError { path: String },
13
14    /// JWT相关错误
15    #[error("JWT错误: {message}")]
16    JwtError { message: String },
17
18    /// JWT密钥错误
19    #[error("JWT密钥无效: {details}")]
20    JwtKeyError { details: String },
21
22    /// JWT签名错误
23    #[error("JWT签名失败: {details}")]
24    JwtSignError { details: String },
25
26    /// JWT验证错误
27    #[error("JWT验证失败: {details}")]
28    JwtVerifyError { details: String },
29
30    /// JWT过期错误
31    #[error("JWT已过期")]
32    JwtExpiredError,
33
34    /// JWT缺少必要字段错误
35    #[error("JWT缺少必要字段: {field}")]
36    JwtMissingFieldError { field: String },
37
38    /// Snowflake相关错误
39    #[error("Snowflake初始化错误: {details}")]
40    SnowflakeInitError { details: String },
41
42    /// Snowflake生成ID错误
43    #[error("Snowflake生成ID失败: {details}")]
44    SnowflakeGenerateError { details: String },
45
46    /// Snowflake配置错误
47    #[error("Snowflake配置无效: {details}")]
48    SnowflakeConfigError { details: String },
49
50    /// 配置管理相关错误
51    #[error("配置加载错误: {details}")]
52    ConfigLoadError { details: String },
53
54    /// 配置文件不存在错误
55    #[error("配置文件不存在: {path}")]
56    ConfigFileNotFoundError { path: String },
57
58    /// 配置解析错误
59    #[error("配置解析失败: {details}")]
60    ConfigParseError { details: String },
61
62    /// 配置验证错误
63    #[error("配置验证失败: {details}")]
64    ConfigValidationError { details: String },
65
66    /// 环境变量解析错误
67    #[error("环境变量解析错误: {details}")]
68    EnvVarParseError { details: String },
69
70    /// 序列化错误
71    #[error("序列化错误: {details}")]
72    SerializationError { details: String },
73
74    /// 反序列化错误
75    #[error("反序列化错误: {details}")]
76    DeserializationError { details: String },
77
78    /// IO错误
79    #[error("IO错误: {details}")]
80    IoError { details: String },
81
82    /// 其他错误
83    #[error("未知错误: {message}")]
84    Other { message: String },
85}
86
87impl From<std::io::Error> for ClamberError {
88    fn from(err: std::io::Error) -> Self {
89        ClamberError::IoError {
90            details: err.to_string(),
91        }
92    }
93}
94
95impl From<serde_json::Error> for ClamberError {
96    fn from(err: serde_json::Error) -> Self {
97        ClamberError::SerializationError {
98            details: err.to_string(),
99        }
100    }
101}
102
103impl From<config::ConfigError> for ClamberError {
104    fn from(err: config::ConfigError) -> Self {
105        match err {
106            config::ConfigError::NotFound(_) => ClamberError::ConfigFileNotFoundError {
107                path: err.to_string(),
108            },
109            _ => ClamberError::ConfigLoadError {
110                details: err.to_string(),
111            },
112        }
113    }
114}
115
116impl From<toml::de::Error> for ClamberError {
117    fn from(err: toml::de::Error) -> Self {
118        ClamberError::ConfigParseError {
119            details: err.to_string(),
120        }
121    }
122}
123
124impl From<serde_yaml::Error> for ClamberError {
125    fn from(err: serde_yaml::Error) -> Self {
126        ClamberError::ConfigParseError {
127            details: err.to_string(),
128        }
129    }
130}
131
132pub type Result<T> = std::result::Result<T, ClamberError>;