Skip to main content

log_full/
error.rs

1//! 错误处理模块
2//! 
3//! 提供统一的错误类型定义和处理机制
4
5/// 日志库的主要错误类型
6#[derive(Debug)]
7pub enum LogError {
8    /// IO 错误
9    Io(std::io::Error),
10    
11    /// 格式化错误
12    Format(std::fmt::Error),
13    
14    /// 配置错误
15    Config { message: String },
16    
17    /// 文件操作错误
18    FileOperation { path: String, reason: String },
19    
20    /// 日志级别解析错误
21    InvalidLogLevel { level: String },
22    
23    /// 初始化错误
24    InitializationError { message: String },
25    
26    /// 解析错误
27    Parse(std::num::ParseIntError),
28    
29    /// 自定义错误
30    Custom { message: String },
31}
32
33impl std::fmt::Display for LogError {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            LogError::Io(err) => write!(f, "IO error: {}", err),
37            LogError::Format(err) => write!(f, "Format error: {}", err),
38            LogError::Config { message } => write!(f, "Configuration error: {}", message),
39            LogError::FileOperation { path, reason } => write!(f, "File operation error: {} - {}", path, reason),
40            LogError::InvalidLogLevel { level } => write!(f, "Invalid log level: {}", level),
41            LogError::InitializationError { message } => write!(f, "Initialization error: {}", message),
42            LogError::Parse(e) => write!(f, "Parse error: {}", e),
43            LogError::Custom { message } => write!(f, "Custom error: {}", message),
44        }
45    }
46}
47
48impl std::error::Error for LogError {}
49
50// 为LogError实现From trait以支持自动类型转换
51impl From<std::io::Error> for LogError {
52    fn from(err: std::io::Error) -> Self {
53        LogError::Io(err)
54    }
55}
56
57impl From<std::fmt::Error> for LogError {
58    fn from(err: std::fmt::Error) -> Self {
59        LogError::Format(err)
60    }
61}
62
63impl From<std::num::ParseIntError> for LogError {
64    fn from(err: std::num::ParseIntError) -> Self {
65        LogError::Parse(err)
66    }
67}
68
69impl From<String> for LogError {
70    fn from(message: String) -> Self {
71        LogError::Custom { message }
72    }
73}
74
75impl From<&str> for LogError {
76    fn from(message: &str) -> Self {
77        LogError::Custom { message: message.to_string() }
78    }
79}
80
81/// 日志库的结果类型
82pub type LogResult<T> = Result<T, LogError>;
83
84/// 错误处理工具函数
85impl LogError {
86    /// 创建配置错误
87    pub fn config<S: Into<String>>(message: S) -> Self {
88        LogError::Config {
89            message: message.into(),
90        }
91    }
92    
93    /// 创建文件操作错误
94    pub fn file_operation<P: Into<String>, R: Into<String>>(path: P, reason: R) -> Self {
95        LogError::FileOperation {
96            path: path.into(),
97            reason: reason.into(),
98        }
99    }
100    
101    /// 创建无效日志级别错误
102    pub fn invalid_log_level<S: Into<String>>(level: S) -> Self {
103        LogError::InvalidLogLevel {
104            level: level.into(),
105        }
106    }
107    
108    /// 创建初始化错误
109    pub fn initialization_error<S: Into<String>>(message: S) -> Self {
110        LogError::InitializationError {
111            message: message.into(),
112        }
113    }
114    
115    /// 创建自定义错误
116    pub fn custom<S: Into<String>>(message: S) -> Self {
117        LogError::Custom {
118            message: message.into(),
119        }
120    }
121    
122    
123}