Skip to main content

yf_common/
error.rs

1//! Error types for yf-common
2
3use thiserror::Error;
4
5/// Common error type for all yf-* tools
6#[derive(Error, Debug)]
7pub enum YfCommonError {
8    #[error("HTTP request failed: {0}")]
9    RequestError(#[from] reqwest::Error),
10
11    #[error("JSON parsing error: {0}")]
12    JsonError(#[from] serde_json::Error),
13
14    #[error("IO error: {0}")]
15    IoError(#[from] std::io::Error),
16
17    #[error("Rate limit exceeded for: {0}")]
18    RateLimitExceeded(String),
19
20    #[error("Server error ({0}): {1}")]
21    ServerError(u16, String),
22
23    #[error("Client error ({0}): {1}")]
24    ClientError(u16, String),
25
26    #[error("Authentication failed: {0}")]
27    AuthError(String),
28
29    #[error("Data parsing error: {0}")]
30    DataError(String),
31
32    #[error("Date parsing error: {0}")]
33    DateParseError(String),
34
35    #[error("Request timeout: {0}")]
36    TimeoutError(String),
37
38    #[error("Max retries exceeded ({0})")]
39    MaxRetriesExceeded(u32),
40
41    #[error("Invalid symbol: {0}")]
42    InvalidSymbol(String),
43
44    #[error("No data available: {0}")]
45    NoData(String),
46}
47
48/// Result type alias using YfCommonError
49pub type Result<T> = std::result::Result<T, YfCommonError>;
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_error_display() {
57        let err = YfCommonError::RateLimitExceeded("test.com".to_string());
58        assert!(err.to_string().contains("Rate limit"));
59    }
60
61    #[test]
62    fn test_error_from_io() {
63        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
64        let err: YfCommonError = io_err.into();
65        assert!(matches!(err, YfCommonError::IoError(_)));
66    }
67
68    #[test]
69    fn test_server_error_display() {
70        let err = YfCommonError::ServerError(500, "Internal Server Error".to_string());
71        let display = err.to_string();
72        assert!(display.contains("500"));
73        assert!(display.contains("Internal Server Error"));
74    }
75
76    #[test]
77    fn test_client_error_display() {
78        let err = YfCommonError::ClientError(404, "Not Found".to_string());
79        let display = err.to_string();
80        assert!(display.contains("404"));
81        assert!(display.contains("Not Found"));
82    }
83
84    #[test]
85    fn test_auth_error_display() {
86        let err = YfCommonError::AuthError("Invalid crumb".to_string());
87        assert!(err.to_string().contains("Invalid crumb"));
88    }
89
90    #[test]
91    fn test_max_retries_error() {
92        let err = YfCommonError::MaxRetriesExceeded(3);
93        let display = err.to_string();
94        assert!(display.contains("3"));
95        assert!(display.contains("Max retries"));
96    }
97
98    #[test]
99    fn test_timeout_error_display() {
100        let err = YfCommonError::TimeoutError("Request timed out".to_string());
101        assert!(err.to_string().contains("timeout"));
102    }
103}