dataforge/
error.rs

1//! 错误处理模块
2
3use thiserror::Error;
4
5/// DataForge 错误类型
6#[derive(Debug, Error)]
7pub enum DataForgeError {
8    #[error("配置错误: {0}")]
9    ConfigError(String),
10
11    #[error("生成器错误: {0}")]
12    GeneratorError(String),
13
14    #[error("正则表达式错误: {0}")]
15    RegexError(#[from] regex::Error),
16
17    #[error("JSON序列化错误: {0}")]
18    JsonError(#[from] serde_json::Error),
19
20    #[error("模板渲染错误: {0}")]
21    TemplateError(#[from] handlebars::RenderError),
22
23    #[cfg(feature = "database")]
24    #[error("数据库操作失败: {0}")]
25    DatabaseError(#[from] sqlx::Error),
26
27    #[error("并发任务超时")]
28    TimeoutError,
29
30    #[error("数据验证失败: {0}")]
31    ValidationError(String),
32
33    #[error("IO错误: {0}")]
34    IoError(#[from] std::io::Error),
35}
36
37/// DataForge 结果类型
38pub type Result<T> = std::result::Result<T, DataForgeError>;
39
40impl DataForgeError {
41    /// 创建配置错误
42    pub fn config<S: Into<String>>(msg: S) -> Self {
43        Self::ConfigError(msg.into())
44    }
45
46    /// 创建生成器错误
47    pub fn generator<S: Into<String>>(msg: S) -> Self {
48        Self::GeneratorError(msg.into())
49    }
50
51    /// 创建验证错误
52    pub fn validation<S: Into<String>>(msg: S) -> Self {
53        Self::ValidationError(msg.into())
54    }
55
56    /// 创建数据库错误
57    #[cfg(feature = "database")]
58    pub fn database<S: Into<String>>(msg: S) -> Self {
59        Self::ConfigError(msg.into()) // 使用配置错误作为备选
60    }
61
62    /// 创建数据库错误(非database feature)
63    #[cfg(not(feature = "database"))]
64    pub fn database<S: Into<String>>(msg: S) -> Self {
65        Self::ConfigError(msg.into())
66    }
67}