1use thiserror::Error;
2
3#[derive(Error, Debug)]
5pub enum ClamberError {
6 #[error("日志系统错误: {message}")]
8 LoggingError { message: String },
9
10 #[error("创建目录失败: {path}")]
12 DirectoryCreationError { path: String },
13
14 #[error("JWT错误: {message}")]
16 JwtError { message: String },
17
18 #[error("JWT密钥无效: {details}")]
20 JwtKeyError { details: String },
21
22 #[error("JWT签名失败: {details}")]
24 JwtSignError { details: String },
25
26 #[error("JWT验证失败: {details}")]
28 JwtVerifyError { details: String },
29
30 #[error("JWT已过期")]
32 JwtExpiredError,
33
34 #[error("JWT缺少必要字段: {field}")]
36 JwtMissingFieldError { field: String },
37
38 #[error("Snowflake初始化错误: {details}")]
40 SnowflakeInitError { details: String },
41
42 #[error("Snowflake生成ID失败: {details}")]
44 SnowflakeGenerateError { details: String },
45
46 #[error("Snowflake配置无效: {details}")]
48 SnowflakeConfigError { details: String },
49
50 #[error("配置加载错误: {details}")]
52 ConfigLoadError { details: String },
53
54 #[error("配置文件不存在: {path}")]
56 ConfigFileNotFoundError { path: String },
57
58 #[error("配置解析失败: {details}")]
60 ConfigParseError { details: String },
61
62 #[error("配置验证失败: {details}")]
64 ConfigValidationError { details: String },
65
66 #[error("环境变量解析错误: {details}")]
68 EnvVarParseError { details: String },
69
70 #[error("序列化错误: {details}")]
72 SerializationError { details: String },
73
74 #[error("反序列化错误: {details}")]
76 DeserializationError { details: String },
77
78 #[error("IO错误: {details}")]
80 IoError { details: String },
81
82 #[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>;