dm_database_sqllog2db/
error.rs

1use std::io;
2use std::path::PathBuf;
3use thiserror::Error;
4
5/// 应用程序错误类型
6#[derive(Debug, Error)]
7pub enum Error {
8    /// Configuration related error
9    #[error("Configuration error: {0}")]
10    Config(#[from] ConfigError),
11
12    /// File operation error
13    #[error("File error: {0}")]
14    File(#[from] FileError),
15
16    /// Database operation error
17    #[error("Database error: {0}")]
18    Database(#[from] DatabaseError),
19
20    /// Parse error
21    #[error("Parse error: {0}")]
22    Parse(#[from] ParseError),
23
24    /// SQL log parser error
25    #[error("SQL log parser error: {0}")]
26    Parser(#[from] ParserError),
27
28    /// Export error
29    #[error("Export error: {0}")]
30    Export(#[from] ExportError),
31
32    /// IO error
33    #[error("IO error: {0}")]
34    Io(#[from] io::Error),
35}
36
37/// 配置错误
38#[derive(Debug, Error)]
39pub enum ConfigError {
40    /// Configuration file not found
41    #[error("Configuration file not found: {0}")]
42    NotFound(PathBuf),
43
44    /// Configuration file parse failed
45    #[error("Failed to parse configuration file {path}: {reason}")]
46    ParseFailed { path: PathBuf, reason: String },
47
48    /// Invalid log level
49    #[error("Invalid log level '{level}', valid values: {}", valid_levels.join(", "))]
50    InvalidLogLevel {
51        level: String,
52        valid_levels: Vec<String>,
53    },
54
55    /// Invalid configuration value
56    #[error("Invalid configuration value {field} = '{value}': {reason}")]
57    InvalidValue {
58        field: String,
59        value: String,
60        reason: String,
61    },
62
63    /// Missing required configuration: no exporters configured
64    #[error("At least one exporter must be configured (database/csv)")]
65    NoExporters,
66}
67
68/// 文件操作错误
69#[derive(Debug, Error)]
70pub enum FileError {
71    /// File already exists
72    #[error("File already exists: {path} (set overwrite=true to replace)")]
73    AlreadyExists { path: PathBuf },
74
75    /// File write failed
76    #[error("Failed to write file {path}: {reason}")]
77    WriteFailed { path: PathBuf, reason: String },
78
79    /// Create directory failed
80    #[error("Failed to create directory {path}: {reason}")]
81    CreateDirectoryFailed { path: PathBuf, reason: String },
82}
83
84/// 数据库错误
85#[derive(Debug, Error)]
86pub enum DatabaseError {
87    /// Database export failed
88    #[error("Database export failed ({table_name}): {reason}")]
89    #[allow(dead_code)]
90    DatabaseExportFailed { table_name: String, reason: String },
91}
92
93/// 解析错误
94#[derive(Debug, Error)]
95pub enum ParseError {}
96
97/// SQL 日志解析器错误
98#[derive(Debug, Error)]
99pub enum ParserError {
100    /// Path not found
101    #[error("Path not found: {path}")]
102    PathNotFound { path: PathBuf },
103
104    /// Invalid path
105    #[error("Invalid path {path}: {reason}")]
106    InvalidPath { path: PathBuf, reason: String },
107
108    /// Read directory failed
109    #[error("Failed to read directory {path}: {reason}")]
110    ReadDirFailed { path: PathBuf, reason: String },
111}
112
113/// 导出错误
114#[derive(Debug, Error)]
115pub enum ExportError {
116    /// CSV export failed
117    #[error("CSV export failed {path}: {reason}")]
118    CsvExportFailed { path: PathBuf, reason: String },
119    /// Failed to create output file
120    #[error("Failed to create output file {path}: {reason}")]
121    FileCreateFailed { path: PathBuf, reason: String },
122
123    /// Failed to write file
124    #[error("Failed to write file {path}: {reason}")]
125    FileWriteFailed { path: PathBuf, reason: String },
126
127    /// Database operation error
128    #[cfg(any(feature = "sqlite", feature = "duckdb", feature = "postgres"))]
129    #[error("Database error: {reason}")]
130    DatabaseError { reason: String },
131
132    /// IO error
133    #[cfg(feature = "dm")]
134    #[error("IO error {path}: {reason}")]
135    IoError { path: PathBuf, reason: String },
136
137    /// External tool error
138    #[cfg(feature = "dm")]
139    #[error("External tool '{tool}' failed: {reason}")]
140    ExternalToolError { tool: String, reason: String },
141}
142
143/// 应用程序 Result 类型别名
144pub type Result<T> = std::result::Result<T, Error>;
145
146// 辅助宏,用于快速创建错误
147#[macro_export]
148macro_rules! config_error {
149    ($variant:ident { $($field:ident: $value:expr),+ $(,)? }) => {
150        $crate::error::Error::Config($crate::error::ConfigError::$variant {
151            $($field: $value),+
152        })
153    };
154}
155
156#[macro_export]
157macro_rules! file_error {
158    ($variant:ident { $($field:ident: $value:expr),+ $(,)? }) => {
159        $crate::error::Error::File($crate::error::FileError::$variant {
160            $($field: $value),+
161        })
162    };
163}
164
165#[macro_export]
166macro_rules! database_error {
167    ($variant:ident { $($field:ident: $value:expr),+ $(,)? }) => {
168        $crate::error::Error::Database($crate::error::DatabaseError::$variant {
169            $($field: $value),+
170        })
171    };
172}
173
174#[macro_export]
175macro_rules! parse_error {
176    ($variant:ident { $($field:ident: $value:expr),+ $(,)? }) => {
177        $crate::error::Error::Parse($crate::error::ParseError::$variant {
178            $($field: $value),+
179        })
180    };
181}
182
183#[macro_export]
184macro_rules! export_error {
185    ($variant:ident { $($field:ident: $value:expr),+ $(,)? }) => {
186        $crate::error::Error::Export($crate::error::ExportError::$variant {
187            $($field: $value),+
188        })
189    };
190}