pub use super::codes::ErrorCode;
pub use super::context::ErrorContext;
pub use super::core::{BuilderKind, ErrorBuilder, ErrorRecord};
pub use super::core::{RecoveryStrategy, RetryPolicy};
pub use super::traits::{ErrorSeverity, ErrorType};
pub use super::{CoreError, ErrorId, SDKResult};
pub use super::traits::{ErrorContextTrait, ErrorFormatTrait, ErrorTrait, FullErrorTrait};
pub use super::core::{
api_error, authentication_error, business_error, configuration_error, network_error,
network_error_with_details, rate_limit_error, serialization_error, service_unavailable_error,
timeout_error, validation_error,
};
pub mod common_imports {
pub use super::*;
pub use std::collections::HashMap;
pub use std::time::Duration;
}
#[macro_export]
macro_rules! network_err {
($msg:expr) => {
$crate::error::network_error($msg)
};
($fmt:expr, $($arg:tt)*) => {
$crate::error::network_error(format!($fmt, $($arg)*))
};
}
#[macro_export]
macro_rules! auth_err {
($msg:expr) => {
$crate::error::authentication_error($msg)
};
($fmt:expr, $($arg:tt)*) => {
$crate::error::authentication_error(format!($fmt, $($arg)*))
};
}
#[macro_export]
macro_rules! validation_err {
($field:expr, $msg:expr) => {
$crate::error::validation_error($field, $msg)
};
($field:expr, $fmt:expr, $($arg:tt)*) => {
$crate::error::validation_error($field, format!($fmt, $($arg)*))
};
}
#[macro_export]
macro_rules! business_err {
($msg:expr) => {
$crate::error::business_error($msg)
};
($fmt:expr, $($arg:tt)*) => {
$crate::error::business_error(format!($fmt, $($arg)*))
};
}
#[macro_export]
macro_rules! api_err {
($status:expr, $endpoint:expr, $msg:expr) => {
$crate::error::api_error($status, $endpoint, $msg, None::<String>)
};
($status:expr, $endpoint:expr, $fmt:expr, $($arg:tt)*) => {
$crate::error::api_error($status, $endpoint, format!($fmt, $($arg)*), None::<String>)
};
}
#[macro_export]
macro_rules! ensure {
($condition:expr, $error:expr) => {
if !$condition {
return Err(Into::<$crate::error::CoreError>::into($error));
}
};
($condition:expr, $fmt:expr, $($arg:tt)*) => {
if !$condition {
return Err(Into::<$crate::error::CoreError>::into(
$crate::error::validation_error("condition", format!($fmt, $($arg)*))
));
}
};
}
#[macro_export]
macro_rules! validate {
($field:expr, $value:expr, $error_msg:expr) => {
if !$value {
return Err(Into::<$crate::error::CoreError>::into(
$crate::error::validation_error($field, $error_msg)
));
}
};
($field:expr, $value:expr, $error_msg:expr, $($arg:tt)*) => {
if !$value {
return Err(Into::<$crate::error::CoreError>::into(
$crate::error::validation_error($field, format!($error_msg, $($arg)*))
));
}
};
}
#[macro_export]
macro_rules! handle_error {
($result:expr, {
$pattern:pat => $handler:expr,
* => $default_handler:expr,
}) => {
match $result {
Ok(value) => value,
Err($pattern) => $handler,
Err(error) => $default_handler(error),
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_modern_prelude_imports() {
let _error: CoreError = network_error("测试");
let _result: SDKResult<()> = Ok(());
let _severity: ErrorSeverity = ErrorSeverity::Warning;
let _code: ErrorCode = ErrorCode::BadRequest;
let _lark_error: CoreError = network_error("测试");
}
#[test]
fn test_modern_convenience_macros() {
let error = network_err!("连接失败");
assert!(error.is_network_error());
let error = auth_err!("认证失败");
assert!(error.is_auth_error());
let error = validation_err!("email", "格式不正确");
assert!(error.is_validation_error());
let error = api_err!(404, "/api/users", "用户不存在");
assert!(error.is_api_error());
}
#[test]
fn test_builder_macro() {
let error = validation_error("email", "邮箱格式不正确");
assert!(error.is_validation_error());
assert_eq!(error.context().get_context("field"), Some("email"));
}
#[test]
fn test_ensure_macro() {
let result = || -> SDKResult<()> {
ensure!(true, validation_error("test", "不应该失败"));
ensure!(false, validation_error("test", "应该失败"));
Ok(())
}();
assert!(result.is_err());
assert!(result.unwrap_err().is_validation_error());
}
#[test]
fn test_validate_macro() {
let result = || -> SDKResult<()> {
validate!("email", "test@example".contains("@"), "邮箱格式不正确");
Ok(())
}();
assert!(result.is_ok());
let result = || -> SDKResult<()> {
validate!("email", "invalid_email".contains("@"), "邮箱格式不正确");
Ok(())
}();
assert!(result.is_err());
assert!(result.unwrap_err().is_validation_error());
}
#[test]
fn test_modern_error_creation() {
let error = api_error(
404,
"/api/v1/users/123",
"用户不存在",
Some("req-123".to_string()),
);
assert!(error.is_api_error());
assert_eq!(error.severity(), ErrorSeverity::Warning);
assert!(!error.is_retryable());
assert!(!error.is_user_error());
assert_eq!(error.context().request_id(), Some("req-123"));
}
}