use thiserror::Error;
#[derive(Debug, Error)]
pub enum BaseError {
#[error("插件已注册: {0}")]
PluginAlreadyRegistered(String),
#[error("插件未找到: {0}")]
PluginNotFound(String),
#[error("插件注册失败 [{0}]: {1}")]
PluginRegisterFailed(String, String),
#[error("插件初始化失败 [{0}]: {1}")]
PluginInitFailed(String, String),
#[error("插件依赖缺失 [{0}]: 缺少依赖 {1}")]
PluginDependencyMissing(String, String),
#[error("插件循环依赖: {0}")]
PluginCircularDependency(String),
#[error("插件配置无效 [{0}]: {1}")]
PluginConfigInvalid(String, String),
#[error("插件关闭失败 [{0}]: {1}")]
PluginShutdownFailed(String, String),
#[error("数据库连接失败: {0}")]
DatabaseConnectionFailed(String),
#[error("数据库已初始化")]
DatabaseAlreadyInitialized,
#[error("数据库查询失败: {0}")]
DatabaseQueryFailed(String),
#[error("数据库执行失败: {0}")]
DatabaseExecuteFailed(String),
#[error("数据库初始化失败: {0}")]
DatabaseInitFailed(String),
#[error("数据库迁移失败 [{0}]: {1}")]
DatabaseMigrationFailed(String, String),
#[error("迁移失败 [{0}] v{1}: {2}")]
MigrationFailed(String, String, String),
#[error("数据库未初始化")]
DatabaseNotInitialized,
#[error("数据库事务失败: {0}")]
DatabaseTransactionFailed(String),
#[error("Redis 连接失败: {0}")]
RedisConnectionFailed(String),
#[error("Redis 已初始化")]
RedisAlreadyInitialized,
#[error("Redis 未初始化")]
RedisNotInitialized,
#[error("Redis 操作失败: {0}")]
RedisOperationFailed(String),
#[error("HTTP 客户端创建失败: {0}")]
HttpClientCreateFailed(String),
#[error("HTTP 请求失败: {0}")]
HttpRequestFailed(String),
#[error("HTTP 响应解析失败: {0}")]
HttpResponseParseFailed(String),
#[error("HTTP 请求超时")]
HttpTimeout,
#[error("Token 密钥无效: {0}")]
TokenKeyInvalid(String),
#[error("Token 生成失败: {0}")]
TokenGenerateFailed(String),
#[error("Token 验证失败: {0}")]
TokenVerifyFailed(String),
#[error("Token 解析失败: {0}")]
TokenParseFailed(String),
#[error("Token 已过期")]
TokenExpired,
#[error("Token 类型无效: {0}")]
TokenTypeInvalid(String),
#[error("JSON 序列化失败: {0}")]
JsonSerializeFailed(String),
#[error("JSON 反序列化失败: {0}")]
JsonDeserializeFailed(String),
#[error("字段类型无效 [{0}]: {1}")]
InvalidFieldType(String, String),
#[error("枚举值无效 [{0}]: 值 '{1}' 不在可选值列表中")]
InvalidEnumValue(String, String),
#[error("字符串长度超出限制 [{0}]: 当前长度 {1},最大长度 {2}")]
StringTooLong(String, usize, usize),
#[error("JSON 格式无效 [{0}]: {1}")]
InvalidJsonFormat(String, String),
#[error("验证失败 [{0}]: {1}")]
ValidationFailed(String, String),
#[error("字段必填: {0}")]
FieldRequired(String),
#[error("字段未找到 [表: {0}]: {1}")]
FieldNotFound(String, String),
#[error("字段权限被拒绝 [表: {0}, 字段: {1}]: {2}")]
FieldPermissionDenied(String, String, String),
#[error("Action 未找到: {0}")]
ActionNotFound(String),
#[error("权限被拒绝: {0}")]
PermissionDenied(String),
#[error("未授权: {0}")]
Unauthorized(String),
#[error("参数缺失: {0}")]
ParamMissing(String),
#[error("参数无效 [{0}]: {1}")]
ParamInvalid(String, String),
#[error("记录未找到: {0}")]
RecordNotFound(String),
#[error("用户未找到: {0}")]
UserNotFound(String),
#[error("密码无效")]
InvalidPassword,
#[error("表配置未设置")]
TableConfigNotSet,
#[error("配置错误: {0}")]
ConfigError(String),
#[error("IO 错误: {0}")]
IoError(String),
#[error("未知错误: {0}")]
Unknown(String),
}
impl From<yang_db::DbError> for BaseError {
fn from(err: yang_db::DbError) -> Self {
BaseError::DatabaseQueryFailed(err.to_string())
}
}
impl From<std::io::Error> for BaseError {
fn from(err: std::io::Error) -> Self {
BaseError::IoError(err.to_string())
}
}
impl From<serde_json::Error> for BaseError {
fn from(err: serde_json::Error) -> Self {
if err.is_syntax() || err.is_data() || err.is_eof() {
BaseError::JsonDeserializeFailed(err.to_string())
} else {
BaseError::JsonSerializeFailed(err.to_string())
}
}
}
impl From<reqwest::Error> for BaseError {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() {
BaseError::HttpTimeout
} else if err.is_connect() {
BaseError::HttpClientCreateFailed(err.to_string())
} else {
BaseError::HttpRequestFailed(err.to_string())
}
}
}
impl BaseError {
pub fn code(&self) -> i32 {
match self {
BaseError::PluginAlreadyRegistered(_) => 100001,
BaseError::PluginNotFound(_) => 100002,
BaseError::PluginRegisterFailed(_, _) => 100003,
BaseError::PluginInitFailed(_, _) => 100004,
BaseError::PluginDependencyMissing(_, _) => 100005,
BaseError::PluginCircularDependency(_) => 100006,
BaseError::PluginConfigInvalid(_, _) => 100007,
BaseError::PluginShutdownFailed(_, _) => 100008,
BaseError::DatabaseConnectionFailed(_) => 200001,
BaseError::DatabaseAlreadyInitialized => 200002,
BaseError::DatabaseQueryFailed(_) => 200003,
BaseError::DatabaseExecuteFailed(_) => 200004,
BaseError::DatabaseInitFailed(_) => 200005,
BaseError::DatabaseMigrationFailed(_, _) => 200006,
BaseError::MigrationFailed(_, _, _) => 200007,
BaseError::DatabaseNotInitialized => 200008,
BaseError::DatabaseTransactionFailed(_) => 200009,
BaseError::RedisConnectionFailed(_) => 210001,
BaseError::RedisAlreadyInitialized => 210002,
BaseError::RedisNotInitialized => 210003,
BaseError::RedisOperationFailed(_) => 210004,
BaseError::HttpClientCreateFailed(_) => 300001,
BaseError::HttpRequestFailed(_) => 300002,
BaseError::HttpResponseParseFailed(_) => 300003,
BaseError::HttpTimeout => 300004,
BaseError::TokenKeyInvalid(_) => 400001,
BaseError::TokenGenerateFailed(_) => 400002,
BaseError::TokenVerifyFailed(_) => 400003,
BaseError::TokenParseFailed(_) => 400004,
BaseError::TokenExpired => 400005,
BaseError::TokenTypeInvalid(_) => 400006,
BaseError::JsonSerializeFailed(_) => 500001,
BaseError::JsonDeserializeFailed(_) => 500002,
BaseError::InvalidFieldType(_, _) => 600001,
BaseError::InvalidEnumValue(_, _) => 600002,
BaseError::StringTooLong(_, _, _) => 600003,
BaseError::InvalidJsonFormat(_, _) => 600004,
BaseError::ValidationFailed(_, _) => 600005,
BaseError::FieldRequired(_) => 600006,
BaseError::FieldNotFound(_, _) => 600007,
BaseError::FieldPermissionDenied(_, _, _) => 600008,
BaseError::ActionNotFound(_) => 700001,
BaseError::PermissionDenied(_) => 700002,
BaseError::Unauthorized(_) => 700003,
BaseError::ParamMissing(_) => 700004,
BaseError::ParamInvalid(_, _) => 700005,
BaseError::RecordNotFound(_) => 700006,
BaseError::UserNotFound(_) => 700007,
BaseError::InvalidPassword => 700008,
BaseError::TableConfigNotSet => 700009,
BaseError::ConfigError(_) => 900001,
BaseError::IoError(_) => 900002,
BaseError::Unknown(_) => 999999,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_codes_plugin() {
assert_eq!(
BaseError::PluginAlreadyRegistered("test".to_string()).code(),
100001
);
assert_eq!(BaseError::PluginNotFound("test".to_string()).code(), 100002);
assert_eq!(
BaseError::PluginRegisterFailed("test".to_string(), "reason".to_string()).code(),
100003
);
assert_eq!(
BaseError::PluginInitFailed("test".to_string(), "reason".to_string()).code(),
100004
);
assert_eq!(
BaseError::PluginDependencyMissing("test".to_string(), "dep".to_string()).code(),
100005
);
assert_eq!(
BaseError::PluginCircularDependency("test".to_string()).code(),
100006
);
assert_eq!(
BaseError::PluginConfigInvalid("test".to_string(), "reason".to_string()).code(),
100007
);
assert_eq!(
BaseError::PluginShutdownFailed("test".to_string(), "reason".to_string()).code(),
100008
);
}
#[test]
fn test_error_codes_database() {
assert_eq!(
BaseError::DatabaseConnectionFailed("reason".to_string()).code(),
200001
);
assert_eq!(BaseError::DatabaseAlreadyInitialized.code(), 200002);
assert_eq!(
BaseError::DatabaseQueryFailed("reason".to_string()).code(),
200003
);
assert_eq!(
BaseError::DatabaseExecuteFailed("reason".to_string()).code(),
200004
);
assert_eq!(
BaseError::DatabaseInitFailed("reason".to_string()).code(),
200005
);
assert_eq!(
BaseError::DatabaseMigrationFailed("plugin".to_string(), "reason".to_string()).code(),
200006
);
assert_eq!(
BaseError::MigrationFailed(
"plugin".to_string(),
"v1".to_string(),
"reason".to_string()
)
.code(),
200007
);
assert_eq!(BaseError::DatabaseNotInitialized.code(), 200008);
assert_eq!(
BaseError::DatabaseTransactionFailed("reason".to_string()).code(),
200009
);
}
#[test]
fn test_error_codes_http() {
assert_eq!(
BaseError::HttpClientCreateFailed("reason".to_string()).code(),
300001
);
assert_eq!(
BaseError::HttpRequestFailed("reason".to_string()).code(),
300002
);
assert_eq!(
BaseError::HttpResponseParseFailed("reason".to_string()).code(),
300003
);
assert_eq!(BaseError::HttpTimeout.code(), 300004);
}
#[test]
fn test_error_codes_token() {
assert_eq!(
BaseError::TokenKeyInvalid("reason".to_string()).code(),
400001
);
assert_eq!(
BaseError::TokenGenerateFailed("reason".to_string()).code(),
400002
);
assert_eq!(
BaseError::TokenVerifyFailed("reason".to_string()).code(),
400003
);
assert_eq!(
BaseError::TokenParseFailed("reason".to_string()).code(),
400004
);
assert_eq!(BaseError::TokenExpired.code(), 400005);
assert_eq!(
BaseError::TokenTypeInvalid("reason".to_string()).code(),
400006
);
}
#[test]
fn test_error_codes_serialization() {
assert_eq!(
BaseError::JsonSerializeFailed("reason".to_string()).code(),
500001
);
assert_eq!(
BaseError::JsonDeserializeFailed("reason".to_string()).code(),
500002
);
}
#[test]
fn test_error_codes_field_validation() {
assert_eq!(
BaseError::InvalidFieldType("field".to_string(), "reason".to_string()).code(),
600001
);
assert_eq!(
BaseError::InvalidEnumValue("field".to_string(), "value".to_string()).code(),
600002
);
assert_eq!(
BaseError::StringTooLong("field".to_string(), 100, 50).code(),
600003
);
assert_eq!(
BaseError::InvalidJsonFormat("field".to_string(), "reason".to_string()).code(),
600004
);
assert_eq!(
BaseError::ValidationFailed("field".to_string(), "reason".to_string()).code(),
600005
);
assert_eq!(BaseError::FieldRequired("field".to_string()).code(), 600006);
assert_eq!(
BaseError::FieldNotFound("table".to_string(), "field".to_string()).code(),
600007
);
assert_eq!(
BaseError::FieldPermissionDenied(
"table".to_string(),
"field".to_string(),
"reason".to_string()
)
.code(),
600008
);
}
#[test]
fn test_error_codes_action_system() {
assert_eq!(
BaseError::ActionNotFound("test_action".to_string()).code(),
700001
);
assert_eq!(
BaseError::PermissionDenied("需要管理员权限".to_string()).code(),
700002
);
assert_eq!(
BaseError::Unauthorized("Token 无效".to_string()).code(),
700003
);
assert_eq!(
BaseError::ParamMissing("user_id".to_string()).code(),
700004
);
assert_eq!(
BaseError::ParamInvalid("age".to_string(), "必须是正整数".to_string()).code(),
700005
);
assert_eq!(
BaseError::RecordNotFound("用户 ID: 123".to_string()).code(),
700006
);
assert_eq!(
BaseError::UserNotFound("user_123".to_string()).code(),
700007
);
assert_eq!(BaseError::InvalidPassword.code(), 700008);
assert_eq!(BaseError::TableConfigNotSet.code(), 700009);
}
#[test]
fn test_error_codes_general() {
assert_eq!(BaseError::ConfigError("reason".to_string()).code(), 900001);
assert_eq!(BaseError::IoError("reason".to_string()).code(), 900002);
assert_eq!(BaseError::Unknown("reason".to_string()).code(), 999999);
}
#[test]
fn test_all_error_codes_are_nonzero() {
let errors = vec![
BaseError::PluginNotFound("test".to_string()),
BaseError::DatabaseQueryFailed("test".to_string()),
BaseError::HttpTimeout,
BaseError::TokenExpired,
BaseError::JsonSerializeFailed("test".to_string()),
BaseError::FieldRequired("test".to_string()),
BaseError::ActionNotFound("test".to_string()),
BaseError::PermissionDenied("test".to_string()),
BaseError::Unauthorized("test".to_string()),
BaseError::ParamMissing("test".to_string()),
BaseError::RecordNotFound("test".to_string()),
BaseError::UserNotFound("test".to_string()),
BaseError::InvalidPassword,
BaseError::TableConfigNotSet,
BaseError::Unknown("test".to_string()),
];
for error in errors {
assert_ne!(error.code(), 0, "错误码不应该为 0: {:?}", error);
}
}
}