1use thiserror::Error;
3
4#[derive(Error, Debug)]
6pub enum ClamberError {
7 #[error("日志系统错误: {message}")]
9 LoggingError { message: String },
10
11 #[error("创建目录失败: {path}")]
13 DirectoryCreationError { path: String },
14
15 #[error("JWT错误: {message}")]
17 JwtError { message: String },
18
19 #[error("JWT密钥无效: {details}")]
21 JwtKeyError { details: String },
22
23 #[error("JWT签名失败: {details}")]
25 JwtSignError { details: String },
26
27 #[error("JWT验证失败: {details}")]
29 JwtVerifyError { details: String },
30
31 #[error("JWT已过期")]
33 JwtExpiredError,
34
35 #[error("JWT缺少必要字段: {field}")]
37 JwtMissingFieldError { field: String },
38
39 #[error("Snowflake初始化错误: {details}")]
41 SnowflakeInitError { details: String },
42
43 #[error("Snowflake生成ID失败: {details}")]
45 SnowflakeGenerateError { details: String },
46
47 #[error("Snowflake配置无效: {details}")]
49 SnowflakeConfigError { details: String },
50
51 #[error("配置加载错误: {details}")]
53 ConfigLoadError { details: String },
54
55 #[error("配置文件不存在: {path}")]
57 ConfigFileNotFoundError { path: String },
58
59 #[error("配置解析失败: {details}")]
61 ConfigParseError { details: String },
62
63 #[error("配置验证失败: {details}")]
65 ConfigValidationError { details: String },
66
67 #[error("环境变量解析错误: {details}")]
69 EnvVarParseError { details: String },
70
71 #[error("序列化错误: {details}")]
73 SerializationError { details: String },
74
75 #[error("反序列化错误: {details}")]
77 DeserializationError { details: String },
78
79 #[error("IO错误: {details}")]
81 IoError { details: String },
82
83 #[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>;