clamber_core/
error.rs

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