ricecoder_github/
errors.rs1use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum GitHubError {
8 #[error("GitHub API error: {0}")]
10 ApiError(String),
11
12 #[error("Authentication failed: {0}")]
14 AuthError(String),
15
16 #[error("Rate limit exceeded")]
18 RateLimitExceeded,
19
20 #[error("Not found: {0}")]
22 NotFound(String),
23
24 #[error("Invalid configuration: {0}")]
26 ConfigError(String),
27
28 #[error("Invalid input: {0}")]
30 InvalidInput(String),
31
32 #[error("Serialization error: {0}")]
34 SerializationError(#[from] serde_json::Error),
35
36 #[error("YAML parsing error: {0}")]
38 YamlError(#[from] serde_yaml::Error),
39
40 #[error("IO error: {0}")]
42 IoError(#[from] std::io::Error),
43
44 #[error("GitHub client error: {0}")]
46 OctocrabError(String),
47
48 #[error("Operation timed out")]
50 Timeout,
51
52 #[error("Network error: {0}")]
54 NetworkError(String),
55
56 #[error("Storage error: {0}")]
58 StorageError(String),
59
60 #[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 pub fn api_error(msg: impl Into<String>) -> Self {
89 GitHubError::ApiError(msg.into())
90 }
91
92 pub fn auth_error(msg: impl Into<String>) -> Self {
94 GitHubError::AuthError(msg.into())
95 }
96
97 pub fn config_error(msg: impl Into<String>) -> Self {
99 GitHubError::ConfigError(msg.into())
100 }
101
102 pub fn not_found(msg: impl Into<String>) -> Self {
104 GitHubError::NotFound(msg.into())
105 }
106
107 pub fn invalid_input(msg: impl Into<String>) -> Self {
109 GitHubError::InvalidInput(msg.into())
110 }
111
112 pub fn network_error(msg: impl Into<String>) -> Self {
114 GitHubError::NetworkError(msg.into())
115 }
116
117 pub fn storage_error(msg: impl Into<String>) -> Self {
119 GitHubError::StorageError(msg.into())
120 }
121
122 pub fn is_rate_limit(&self) -> bool {
124 matches!(self, GitHubError::RateLimitExceeded)
125 }
126
127 pub fn is_auth_error(&self) -> bool {
129 matches!(self, GitHubError::AuthError(_))
130 }
131
132 pub fn is_not_found(&self) -> bool {
134 matches!(self, GitHubError::NotFound(_))
135 }
136}
137
138pub type Result<T> = std::result::Result<T, GitHubError>;