Skip to main content

apr_qa_report/
error.rs

1//! Error types for apr-qa-report
2
3use thiserror::Error;
4
5/// Result type alias
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during report generation
9#[derive(Debug, Error)]
10pub enum Error {
11    /// IO error (from std::io)
12    #[error("IO error: {0}")]
13    IoError(#[from] std::io::Error),
14
15    /// IO error with custom message
16    #[error("IO error: {0}")]
17    Io(String),
18
19    /// Serialization error
20    #[error("Serialization error: {0}")]
21    SerializationError(#[from] serde_json::Error),
22
23    /// Invalid score calculation
24    #[error("Invalid score calculation: {0}")]
25    InvalidScore(String),
26
27    /// Validation error
28    #[error("Validation error: {0}")]
29    Validation(String),
30
31    /// Template rendering error
32    #[error("Template error: {0}")]
33    TemplateError(String),
34
35    /// Gateway check failed
36    #[error("Gateway {gate} failed: {reason}")]
37    GatewayFailed {
38        /// Gateway identifier
39        gate: String,
40        /// Failure reason
41        reason: String,
42    },
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_error_display() {
51        let err = Error::GatewayFailed {
52            gate: "G1".to_string(),
53            reason: "Model failed to load".to_string(),
54        };
55        assert!(err.to_string().contains("G1"));
56        assert!(err.to_string().contains("Model failed to load"));
57    }
58
59    #[test]
60    fn test_invalid_score() {
61        let err = Error::InvalidScore("Negative weight".to_string());
62        assert!(err.to_string().contains("Negative weight"));
63    }
64
65    #[test]
66    fn test_io_error() {
67        let err = Error::Io("File not found".to_string());
68        assert!(err.to_string().contains("File not found"));
69    }
70
71    #[test]
72    fn test_validation_error() {
73        let err = Error::Validation("Invalid CSV field".to_string());
74        assert!(err.to_string().contains("Invalid CSV field"));
75    }
76
77    #[test]
78    fn test_template_error() {
79        let err = Error::TemplateError("Missing variable".to_string());
80        assert!(err.to_string().contains("Missing variable"));
81    }
82}