Skip to main content

flare_core/common/error/
mod.rs

1//! Flare 错误处理模块
2//!
3//! 提供统一的错误处理机制,支持国际化、错误代码分类和错误转换
4//!
5//! ## 模块结构
6//!
7//! - `code` - 错误代码和错误类别定义
8//! - `localized` - 国际化错误信息结构
9//! - `flare_error` - Flare IM 统一错误类型
10//! - `builder` - 错误构建器(链式 API)
11//! - `conversions` - 错误类型转换实现
12
13pub mod builder;
14pub mod code;
15mod conversions;
16pub mod flare_error;
17pub mod localized;
18
19// 重新导出公共类型和函数
20pub use builder::ErrorBuilder;
21pub use code::{ErrorCategory, ErrorCode};
22pub use flare_error::{ClientError, FlareError, Result, ServerError};
23pub use localized::LocalizedError;
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn test_error_code() {
31        assert_eq!(ErrorCode::ConnectionFailed.as_u32(), 1000);
32        assert_eq!(ErrorCode::AuthenticationFailed.as_u32(), 2000);
33        assert_eq!(ErrorCode::ConnectionFailed.as_str(), "CONNECTION_FAILED");
34    }
35
36    #[test]
37    fn test_error_code_from_u32() {
38        assert_eq!(ErrorCode::from_u32(1000), Some(ErrorCode::ConnectionFailed));
39        assert_eq!(ErrorCode::from_u32(9999), Some(ErrorCode::UnknownError));
40        assert_eq!(ErrorCode::from_u32(99999), None);
41    }
42
43    #[test]
44    fn test_error_category() {
45        assert_eq!(
46            ErrorCode::ConnectionFailed.category(),
47            ErrorCategory::Connection
48        );
49        assert_eq!(
50            ErrorCode::AuthenticationFailed.category(),
51            ErrorCategory::Authentication
52        );
53        assert_eq!(
54            ErrorCode::SerializationError.category(),
55            ErrorCategory::Serialization
56        );
57    }
58
59    #[test]
60    fn test_error_retryable() {
61        assert!(ErrorCode::ConnectionTimeout.is_retryable());
62        assert!(ErrorCode::NetworkTimeout.is_retryable());
63        assert!(!ErrorCode::AuthenticationFailed.is_retryable());
64    }
65
66    #[test]
67    fn test_localized_error() {
68        let error = LocalizedError::new(ErrorCode::UserNotFound, "用户不存在")
69            .with_param("user_id", "user123")
70            .with_details("详细错误信息");
71
72        assert_eq!(error.code, ErrorCode::UserNotFound);
73        assert_eq!(error.reason, "用户不存在");
74        assert_eq!(error.details, Some("详细错误信息".to_string()));
75        assert_eq!(
76            error.params.as_ref().unwrap().get("user_id"),
77            Some(&"user123".to_string())
78        );
79        assert!(!error.is_retryable());
80    }
81
82    #[test]
83    fn test_flare_error() {
84        let error = FlareError::user_not_found("user123");
85
86        assert_eq!(error.code(), Some(ErrorCode::UserNotFound));
87        assert_eq!(error.reason(), "用户不存在");
88        assert!(!error.is_retryable());
89    }
90
91    #[test]
92    fn test_error_builder() {
93        let error = ErrorBuilder::new(ErrorCode::MessageSendFailed, "消息发送失败")
94            .param("message_id", "msg123")
95            .param("user_id", "user456")
96            .details("网络连接中断")
97            .build();
98
99        assert_eq!(error.code(), Some(ErrorCode::MessageSendFailed));
100        assert_eq!(error.reason(), "消息发送失败");
101    }
102
103    #[test]
104    fn test_error_conversion() {
105        let io_err = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "连接被拒绝");
106        let flare_err: FlareError = io_err.into();
107        assert!(flare_err.reason().contains("连接被拒绝"));
108    }
109}