dm_database_sqllog2db/
error.rs1use std::io;
2use std::path::PathBuf;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum Error {
8 #[error("Configuration error: {0}")]
10 Config(#[from] ConfigError),
11
12 #[error("File error: {0}")]
14 File(#[from] FileError),
15
16 #[error("Database error: {0}")]
18 Database(#[from] DatabaseError),
19
20 #[error("Parse error: {0}")]
22 Parse(#[from] ParseError),
23
24 #[error("SQL log parser error: {0}")]
26 Parser(#[from] ParserError),
27
28 #[error("Export error: {0}")]
30 Export(#[from] ExportError),
31
32 #[error("IO error: {0}")]
34 Io(#[from] io::Error),
35}
36
37#[derive(Debug, Error)]
39pub enum ConfigError {
40 #[error("Configuration file not found: {0}")]
42 NotFound(PathBuf),
43
44 #[error("Failed to parse configuration file {path}: {reason}")]
46 ParseFailed { path: PathBuf, reason: String },
47
48 #[error("Invalid log level '{level}', valid values: {}", valid_levels.join(", "))]
50 InvalidLogLevel {
51 level: String,
52 valid_levels: Vec<String>,
53 },
54
55 #[error("Invalid configuration value {field} = '{value}': {reason}")]
57 InvalidValue {
58 field: String,
59 value: String,
60 reason: String,
61 },
62
63 #[error("At least one exporter must be configured (database/csv)")]
65 NoExporters,
66}
67
68#[derive(Debug, Error)]
70pub enum FileError {
71 #[error("File already exists: {path} (set overwrite=true to replace)")]
73 AlreadyExists { path: PathBuf },
74
75 #[error("Failed to write file {path}: {reason}")]
77 WriteFailed { path: PathBuf, reason: String },
78
79 #[error("Failed to create directory {path}: {reason}")]
81 CreateDirectoryFailed { path: PathBuf, reason: String },
82}
83
84#[derive(Debug, Error)]
86pub enum DatabaseError {
87 #[error("Database export failed ({table_name}): {reason}")]
89 #[allow(dead_code)]
90 DatabaseExportFailed { table_name: String, reason: String },
91}
92
93#[derive(Debug, Error)]
95pub enum ParseError {}
96
97#[derive(Debug, Error)]
99pub enum ParserError {
100 #[error("Path not found: {path}")]
102 PathNotFound { path: PathBuf },
103
104 #[error("Invalid path {path}: {reason}")]
106 InvalidPath { path: PathBuf, reason: String },
107
108 #[error("Failed to read directory {path}: {reason}")]
110 ReadDirFailed { path: PathBuf, reason: String },
111}
112
113#[derive(Debug, Error)]
115pub enum ExportError {
116 #[error("CSV export failed {path}: {reason}")]
118 CsvExportFailed { path: PathBuf, reason: String },
119 #[error("Failed to create output file {path}: {reason}")]
121 FileCreateFailed { path: PathBuf, reason: String },
122
123 #[error("Failed to write file {path}: {reason}")]
125 FileWriteFailed { path: PathBuf, reason: String },
126
127 #[cfg(any(feature = "sqlite", feature = "duckdb", feature = "postgres"))]
129 #[error("Database error: {reason}")]
130 DatabaseError { reason: String },
131
132 #[cfg(feature = "dm")]
134 #[error("IO error {path}: {reason}")]
135 IoError { path: PathBuf, reason: String },
136
137 #[cfg(feature = "dm")]
139 #[error("External tool '{tool}' failed: {reason}")]
140 ExternalToolError { tool: String, reason: String },
141}
142
143pub type Result<T> = std::result::Result<T, Error>;
145
146#[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}