1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Error types for devboy-tools.
//!
//! This module provides a unified error handling system that works across
//! all providers and components.
use thiserror::Error;
/// Main error type for devboy operations.
#[derive(Error, Debug)]
pub enum Error {
// =========================================================================
// HTTP / Network Errors
// =========================================================================
/// HTTP request failed
#[error("HTTP error: {0}")]
Http(String),
/// Network connectivity error
#[error("Network error: {0}")]
Network(String),
#[error("Request timeout")]
Timeout,
// =========================================================================
// Authentication / Authorization Errors
// =========================================================================
/// 401 Unauthorized - invalid or missing credentials
#[error("Unauthorized: {0}")]
Unauthorized(String),
/// 403 Forbidden - valid credentials but insufficient permissions
#[error("Forbidden: {0}")]
Forbidden(String),
// =========================================================================
// API Errors
// =========================================================================
/// API returned an error response
#[error("API error ({status}): {message}")]
Api {
/// HTTP status code
status: u16,
/// Error message from API
message: String,
},
/// Resource not found (404)
#[error("Not found: {0}")]
NotFound(String),
/// Rate limit exceeded (429)
#[error("Rate limit exceeded: retry after {retry_after:?}s")]
RateLimited {
/// Seconds to wait before retry
retry_after: Option<u64>,
},
/// Server error (5xx)
#[error("Server error ({status}): {message}")]
ServerError {
/// HTTP status code
status: u16,
/// Error message
message: String,
},
// =========================================================================
// Data Errors
// =========================================================================
/// Serialization/deserialization failed
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
/// Invalid data format or content
#[error("Invalid data: {0}")]
InvalidData(String),
// =========================================================================
// Configuration Errors
// =========================================================================
/// Configuration error
#[error("Configuration error: {0}")]
Config(String),
/// Missing required configuration
#[error("Missing configuration: {0}")]
MissingConfig(String),
// =========================================================================
// Storage Errors
// =========================================================================
/// Storage/keychain error
#[error("Storage error: {0}")]
Storage(String),
/// Credential not found in keychain
#[error("Credential not found: {provider}/{key}")]
CredentialNotFound { provider: String, key: String },
// =========================================================================
// Provider Errors
// =========================================================================
/// Provider not found or not configured
#[error("Provider not found: {0}")]
ProviderNotFound(String),
/// Provider not supported for this operation
#[error("Provider '{provider}' does not support: {operation}")]
ProviderUnsupported {
provider: String,
/// Unsupported operation
operation: String,
},
// =========================================================================
// Generic Errors
// =========================================================================
/// IO error
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
/// Generic error wrapper
#[error("{0}")]
Other(#[from] anyhow::Error),
}
impl Error {
/// Create an API error from HTTP status and message.
pub fn from_status(status: u16, message: impl Into<String>) -> Self {
let message = message.into();
match status {
401 => Error::Unauthorized(message),
403 => Error::Forbidden(message),
404 => Error::NotFound(message),
429 => Error::RateLimited { retry_after: None },
500..=599 => Error::ServerError { status, message },
_ => Error::Api { status, message },
}
}
/// Check if this is a retryable error.
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::Timeout
| Error::Network(_)
| Error::RateLimited { .. }
| Error::ServerError { .. }
)
}
/// Check if this is an authentication error.
pub fn is_auth_error(&self) -> bool {
matches!(self, Error::Unauthorized(_) | Error::Forbidden(_))
}
}
/// Result type alias for devboy operations.
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_status() {
assert!(matches!(
Error::from_status(401, "test"),
Error::Unauthorized(_)
));
assert!(matches!(
Error::from_status(403, "test"),
Error::Forbidden(_)
));
assert!(matches!(
Error::from_status(404, "test"),
Error::NotFound(_)
));
assert!(matches!(
Error::from_status(429, "test"),
Error::RateLimited { .. }
));
assert!(matches!(
Error::from_status(500, "test"),
Error::ServerError { .. }
));
assert!(matches!(Error::from_status(400, "test"), Error::Api { .. }));
}
#[test]
fn test_is_retryable() {
assert!(Error::Timeout.is_retryable());
assert!(Error::Network("test".into()).is_retryable());
assert!(Error::RateLimited { retry_after: None }.is_retryable());
assert!(
Error::ServerError {
status: 500,
message: "test".into()
}
.is_retryable()
);
assert!(!Error::Unauthorized("test".into()).is_retryable());
assert!(!Error::NotFound("test".into()).is_retryable());
}
#[test]
fn test_is_auth_error() {
assert!(Error::Unauthorized("test".into()).is_auth_error());
assert!(Error::Forbidden("test".into()).is_auth_error());
assert!(!Error::NotFound("test".into()).is_auth_error());
}
}