use thiserror::Error;
#[derive(Error, Debug)]
pub enum ClamberError {
#[error("日志系统错误: {message}")]
LoggingError { message: String },
#[error("创建目录失败: {path}")]
DirectoryCreationError { path: String },
#[error("JWT错误: {message}")]
JwtError { message: String },
#[error("JWT密钥无效: {details}")]
JwtKeyError { details: String },
#[error("JWT签名失败: {details}")]
JwtSignError { details: String },
#[error("JWT验证失败: {details}")]
JwtVerifyError { details: String },
#[error("JWT已过期")]
JwtExpiredError,
#[error("JWT缺少必要字段: {field}")]
JwtMissingFieldError { field: String },
#[error("Snowflake初始化错误: {details}")]
SnowflakeInitError { details: String },
#[error("Snowflake生成ID失败: {details}")]
SnowflakeGenerateError { details: String },
#[error("Snowflake配置无效: {details}")]
SnowflakeConfigError { details: String },
#[error("配置加载错误: {details}")]
ConfigLoadError { details: String },
#[error("配置文件不存在: {path}")]
ConfigFileNotFoundError { path: String },
#[error("配置解析失败: {details}")]
ConfigParseError { details: String },
#[error("配置验证失败: {details}")]
ConfigValidationError { details: String },
#[error("环境变量解析错误: {details}")]
EnvVarParseError { details: String },
#[error("序列化错误: {details}")]
SerializationError { details: String },
#[error("反序列化错误: {details}")]
DeserializationError { details: String },
#[error("IO错误: {details}")]
IoError { details: String },
#[error("未知错误: {message}")]
Other { message: String },
}
impl From<std::io::Error> for ClamberError {
fn from(err: std::io::Error) -> Self {
ClamberError::IoError {
details: err.to_string(),
}
}
}
impl From<serde_json::Error> for ClamberError {
fn from(err: serde_json::Error) -> Self {
ClamberError::SerializationError {
details: err.to_string(),
}
}
}
impl From<config::ConfigError> for ClamberError {
fn from(err: config::ConfigError) -> Self {
match err {
config::ConfigError::NotFound(_) => ClamberError::ConfigFileNotFoundError {
path: err.to_string(),
},
_ => ClamberError::ConfigLoadError {
details: err.to_string(),
},
}
}
}
impl From<toml::de::Error> for ClamberError {
fn from(err: toml::de::Error) -> Self {
ClamberError::ConfigParseError {
details: err.to_string(),
}
}
}
impl From<serde_yaml::Error> for ClamberError {
fn from(err: serde_yaml::Error) -> Self {
ClamberError::ConfigParseError {
details: err.to_string(),
}
}
}
pub type Result<T> = std::result::Result<T, ClamberError>;