tinyetl/
error.rs

1use anyhow::Error as AnyhowError;
2
3#[derive(Debug, thiserror::Error)]
4pub enum TinyEtlError {
5    #[error("Connection error: {0}")]
6    Connection(String),
7
8    #[error("Schema inference error: {0}")]
9    SchemaInference(String),
10
11    #[error("Data transfer error: {0}")]
12    DataTransfer(String),
13
14    #[error("Configuration error: {0}")]
15    Configuration(String),
16
17    #[error("Config error: {0}")] // Alias for Configuration for backwards compatibility
18    Config(String),
19
20    #[error("Transform error: {0}")]
21    Transform(String),
22
23    #[error("Data validation error: {0}")]
24    DataValidation(String),
25
26    #[error("IO error: {0}")]
27    Io(#[from] std::io::Error),
28
29    #[error("CSV parsing error: {0}")]
30    Csv(#[from] csv::Error),
31
32    #[error("JSON parsing error: {0}")]
33    Json(#[from] serde_json::Error),
34
35    #[error("Database error: {0}")]
36    Database(#[from] sqlx::Error),
37
38    #[error("Lua error: {0}")]
39    Lua(#[from] mlua::Error),
40
41    #[error("General error: {0}")]
42    General(#[from] AnyhowError),
43}
44
45pub type Result<T> = std::result::Result<T, TinyEtlError>;
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use std::io;
51
52    #[test]
53    fn test_error_display() {
54        let err = TinyEtlError::Connection("test connection".to_string());
55        assert_eq!(err.to_string(), "Connection error: test connection");
56    }
57
58    #[test]
59    fn test_io_error_conversion() {
60        let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
61        let etl_err = TinyEtlError::from(io_err);
62        assert!(matches!(etl_err, TinyEtlError::Io(_)));
63    }
64
65    #[test]
66    fn test_all_error_types() {
67        let schema_err = TinyEtlError::SchemaInference("bad schema".to_string());
68        assert_eq!(schema_err.to_string(), "Schema inference error: bad schema");
69
70        let transfer_err = TinyEtlError::DataTransfer("transfer failed".to_string());
71        assert_eq!(
72            transfer_err.to_string(),
73            "Data transfer error: transfer failed"
74        );
75
76        let config_err = TinyEtlError::Configuration("bad config".to_string());
77        assert_eq!(config_err.to_string(), "Configuration error: bad config");
78    }
79
80    #[test]
81    fn test_csv_error_conversion() {
82        // Create a proper CSV error through IO error
83        let io_err = std::io::Error::new(std::io::ErrorKind::InvalidData, "CSV parse error");
84        let csv_err = csv::Error::from(io_err);
85        let etl_err = TinyEtlError::from(csv_err);
86        assert!(matches!(etl_err, TinyEtlError::Csv(_)));
87    }
88}