ricecoder_github/
errors.rs

1//! GitHub Integration Error Types
2
3use thiserror::Error;
4
5/// Errors that can occur during GitHub operations
6#[derive(Debug, Error)]
7pub enum GitHubError {
8    /// API error from GitHub
9    #[error("GitHub API error: {0}")]
10    ApiError(String),
11
12    /// Authentication failed
13    #[error("Authentication failed: {0}")]
14    AuthError(String),
15
16    /// Rate limit exceeded
17    #[error("Rate limit exceeded")]
18    RateLimitExceeded,
19
20    /// Resource not found
21    #[error("Not found: {0}")]
22    NotFound(String),
23
24    /// Invalid configuration
25    #[error("Invalid configuration: {0}")]
26    ConfigError(String),
27
28    /// Invalid input
29    #[error("Invalid input: {0}")]
30    InvalidInput(String),
31
32    /// Serialization error
33    #[error("Serialization error: {0}")]
34    SerializationError(#[from] serde_json::Error),
35
36    /// YAML parsing error
37    #[error("YAML parsing error: {0}")]
38    YamlError(#[from] serde_yaml::Error),
39
40    /// IO error
41    #[error("IO error: {0}")]
42    IoError(#[from] std::io::Error),
43
44    /// Octocrab error
45    #[error("GitHub client error: {0}")]
46    OctocrabError(String),
47
48    /// Timeout error
49    #[error("Operation timed out")]
50    Timeout,
51
52    /// Network error
53    #[error("Network error: {0}")]
54    NetworkError(String),
55
56    /// Storage error
57    #[error("Storage error: {0}")]
58    StorageError(String),
59
60    /// Generic error
61    #[error("{0}")]
62    Other(String),
63}
64
65impl Clone for GitHubError {
66    fn clone(&self) -> Self {
67        match self {
68            GitHubError::ApiError(s) => GitHubError::ApiError(s.clone()),
69            GitHubError::AuthError(s) => GitHubError::AuthError(s.clone()),
70            GitHubError::RateLimitExceeded => GitHubError::RateLimitExceeded,
71            GitHubError::NotFound(s) => GitHubError::NotFound(s.clone()),
72            GitHubError::ConfigError(s) => GitHubError::ConfigError(s.clone()),
73            GitHubError::InvalidInput(s) => GitHubError::InvalidInput(s.clone()),
74            GitHubError::SerializationError(e) => GitHubError::Other(format!("Serialization error: {}", e)),
75            GitHubError::YamlError(e) => GitHubError::Other(format!("YAML error: {}", e)),
76            GitHubError::IoError(e) => GitHubError::Other(format!("IO error: {}", e)),
77            GitHubError::OctocrabError(s) => GitHubError::OctocrabError(s.clone()),
78            GitHubError::Timeout => GitHubError::Timeout,
79            GitHubError::NetworkError(s) => GitHubError::NetworkError(s.clone()),
80            GitHubError::StorageError(s) => GitHubError::StorageError(s.clone()),
81            GitHubError::Other(s) => GitHubError::Other(s.clone()),
82        }
83    }
84}
85
86impl GitHubError {
87    /// Create a new API error
88    pub fn api_error(msg: impl Into<String>) -> Self {
89        GitHubError::ApiError(msg.into())
90    }
91
92    /// Create a new auth error
93    pub fn auth_error(msg: impl Into<String>) -> Self {
94        GitHubError::AuthError(msg.into())
95    }
96
97    /// Create a new config error
98    pub fn config_error(msg: impl Into<String>) -> Self {
99        GitHubError::ConfigError(msg.into())
100    }
101
102    /// Create a new not found error
103    pub fn not_found(msg: impl Into<String>) -> Self {
104        GitHubError::NotFound(msg.into())
105    }
106
107    /// Create a new invalid input error
108    pub fn invalid_input(msg: impl Into<String>) -> Self {
109        GitHubError::InvalidInput(msg.into())
110    }
111
112    /// Create a new network error
113    pub fn network_error(msg: impl Into<String>) -> Self {
114        GitHubError::NetworkError(msg.into())
115    }
116
117    /// Create a new storage error
118    pub fn storage_error(msg: impl Into<String>) -> Self {
119        GitHubError::StorageError(msg.into())
120    }
121
122    /// Check if this is a rate limit error
123    pub fn is_rate_limit(&self) -> bool {
124        matches!(self, GitHubError::RateLimitExceeded)
125    }
126
127    /// Check if this is an auth error
128    pub fn is_auth_error(&self) -> bool {
129        matches!(self, GitHubError::AuthError(_))
130    }
131
132    /// Check if this is a not found error
133    pub fn is_not_found(&self) -> bool {
134        matches!(self, GitHubError::NotFound(_))
135    }
136}
137
138/// Result type for GitHub operations
139pub type Result<T> = std::result::Result<T, GitHubError>;