Skip to main content

google_search_console_api/error/
mod.rs

1//! Error types for the Google Search Console API client.
2
3use std::fmt::{Debug, Display, Formatter};
4
5/// Error type for Google Search Console API operations.
6///
7/// This enum represents all possible errors that can occur when using this library.
8#[derive(Clone)]
9pub enum GoogleApiError {
10    /// HTTP connection error (network issues, DNS resolution, etc.).
11    Connection(String),
12    /// JSON parsing error (invalid response format).
13    JsonParse(String),
14    /// API error response with status code and message.
15    Api {
16        /// HTTP status code.
17        status: u16,
18        /// Error message from the API.
19        message: String,
20    },
21    /// Authentication error (invalid or expired token).
22    Auth(String),
23}
24
25impl Display for GoogleApiError {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        match self {
28            GoogleApiError::Connection(e) => write!(f, "Connection error: {}", e),
29            GoogleApiError::JsonParse(e) => write!(f, "JSON parse error: {}", e),
30            GoogleApiError::Api { status, message } => {
31                write!(f, "API error ({}): {}", status, message)
32            }
33            GoogleApiError::Auth(e) => write!(f, "Authentication error: {}", e),
34        }
35    }
36}
37
38impl Debug for GoogleApiError {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        Display::fmt(self, f)
41    }
42}
43
44impl std::error::Error for GoogleApiError {}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_connection_error_display() {
52        let error = GoogleApiError::Connection("timeout".to_string());
53        assert_eq!(format!("{}", error), "Connection error: timeout");
54    }
55
56    #[test]
57    fn test_json_parse_error_display() {
58        let error = GoogleApiError::JsonParse("invalid json".to_string());
59        assert_eq!(format!("{}", error), "JSON parse error: invalid json");
60    }
61
62    #[test]
63    fn test_api_error_display() {
64        let error = GoogleApiError::Api {
65            status: 403,
66            message: "Forbidden".to_string(),
67        };
68        assert_eq!(format!("{}", error), "API error (403): Forbidden");
69    }
70
71    #[test]
72    fn test_auth_error_display() {
73        let error = GoogleApiError::Auth("token expired".to_string());
74        assert_eq!(format!("{}", error), "Authentication error: token expired");
75    }
76
77    #[test]
78    fn test_error_debug() {
79        let error = GoogleApiError::Connection("test".to_string());
80        assert_eq!(format!("{:?}", error), "Connection error: test");
81    }
82
83    #[test]
84    fn test_error_clone() {
85        let error = GoogleApiError::Api {
86            status: 404,
87            message: "Not Found".to_string(),
88        };
89        let cloned = error.clone();
90        assert_eq!(format!("{}", error), format!("{}", cloned));
91    }
92
93    #[test]
94    fn test_error_is_std_error() {
95        let error: Box<dyn std::error::Error> =
96            Box::new(GoogleApiError::Connection("test".to_string()));
97        assert!(error.to_string().contains("Connection error"));
98    }
99}