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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Comprehensive error handling for the AIX library.
//!
//! This module provides a unified error type that can represent all possible
//! failure modes when interacting with AI providers.
use std::error::Error as StdError;
use std::fmt;
use std::time::Duration;
/// Unified error type for all AIX operations.
#[derive(Debug, Clone)]
pub enum AixError {
/// Transport layer errors (network, DNS, etc.)
Transport {
/// The underlying error
source: String,
/// Additional context about the error
context: String,
},
/// Provider-specific errors (API errors, validation, etc.)
Provider {
/// Name of the provider that returned the error
provider: String,
/// Error code from the provider (if available)
code: Option<String>,
/// Human-readable error message
message: String,
/// HTTP status code (if applicable)
status: Option<u16>,
},
/// Rate limiting errors
RateLimit {
/// Name of the provider that rate limited the request
provider: String,
/// Suggested retry delay (if provided by the provider)
retry_after: Option<Duration>,
/// Human-readable error message
message: String,
},
/// Serialization/deserialization errors
Serialization {
/// The underlying error
source: String,
/// Additional context about what was being serialized/deserialized
context: String,
},
/// Configuration errors
Config {
/// Human-readable error message
message: String,
},
/// Streaming-related errors
Stream {
/// Human-readable error message
message: String,
/// The underlying error (if available)
source: Option<String>,
},
/// Safety/content policy violations
Safety {
/// Name of the provider that flagged the content
provider: String,
/// Category of safety violation
category: String,
/// Human-readable error message
message: String,
},
/// Authentication/authorization errors
Auth {
/// Name of the provider that rejected the authentication
provider: String,
/// Human-readable error message
message: String,
},
/// Timeout errors
Timeout {
/// The operation that timed out
operation: String,
/// How long we waited before timing out
duration: Duration,
},
/// Catch-all for other errors
Other {
/// Human-readable error message
message: String,
/// The underlying error (if available)
source: Option<String>,
},
}
impl AixError {
/// Check if this error is retryable.
///
/// Returns true if the error warrants a retry attempt.
pub fn is_retryable(&self) -> bool {
match self {
AixError::Transport { .. } => true,
AixError::Provider { status, .. } => {
status.map_or(false, |s| s >= 500 || s == 429)
}
AixError::RateLimit { .. } => true,
AixError::Timeout { .. } => true,
AixError::Serialization { .. } => false,
AixError::Config { .. } => false,
AixError::Stream { .. } => false,
AixError::Safety { .. } => false,
AixError::Auth { .. } => false,
AixError::Other { .. } => false,
}
}
/// Create a new transport error.
pub fn transport<S: Into<String>, C: Into<String>>(source: S, context: C) -> Self {
AixError::Transport {
source: source.into(),
context: context.into(),
}
}
/// Create a new provider error.
pub fn provider<P: Into<String>, M: Into<String>>(
provider: P,
message: M,
) -> Self {
AixError::Provider {
provider: provider.into(),
code: None,
message: message.into(),
status: None,
}
}
/// Create a new provider error with status and code.
pub fn provider_with_details<P: Into<String>, M: Into<String>, C: Into<String>>(
provider: P,
message: M,
status: u16,
code: C,
) -> Self {
AixError::Provider {
provider: provider.into(),
code: Some(code.into()),
message: message.into(),
status: Some(status),
}
}
/// Create a new rate limit error.
pub fn rate_limit<P: Into<String>, M: Into<String>>(
provider: P,
message: M,
) -> Self {
AixError::RateLimit {
provider: provider.into(),
retry_after: None,
message: message.into(),
}
}
/// Create a new rate limit error with retry after.
pub fn rate_limit_with_retry<P: Into<String>, M: Into<String>>(
provider: P,
message: M,
retry_after: Duration,
) -> Self {
AixError::RateLimit {
provider: provider.into(),
retry_after: Some(retry_after),
message: message.into(),
}
}
/// Create a new serialization error.
pub fn serialization<S: Into<String>, C: Into<String>>(source: S, context: C) -> Self {
AixError::Serialization {
source: source.into(),
context: context.into(),
}
}
/// Create a new config error.
pub fn config<M: Into<String>>(message: M) -> Self {
AixError::Config {
message: message.into(),
}
}
/// Create a new stream error.
pub fn stream<M: Into<String>>(message: M) -> Self {
AixError::Stream {
message: message.into(),
source: None,
}
}
/// Create a new stream error with source.
pub fn stream_with_source<M: Into<String>, S: Into<String>>(
message: M,
source: S,
) -> Self {
AixError::Stream {
message: message.into(),
source: Some(source.into()),
}
}
/// Create a new safety error.
pub fn safety<P: Into<String>, C: Into<String>, M: Into<String>>(
provider: P,
category: C,
message: M,
) -> Self {
AixError::Safety {
provider: provider.into(),
category: category.into(),
message: message.into(),
}
}
/// Create a new auth error.
pub fn auth<P: Into<String>, M: Into<String>>(provider: P, message: M) -> Self {
AixError::Auth {
provider: provider.into(),
message: message.into(),
}
}
/// Create a new timeout error.
pub fn timeout<O: Into<String>>(operation: O, duration: Duration) -> Self {
AixError::Timeout {
operation: operation.into(),
duration,
}
}
/// Create a new other error.
pub fn other<M: Into<String>>(message: M) -> Self {
AixError::Other {
message: message.into(),
source: None,
}
}
/// Create a new other error with source.
pub fn other_with_source<M: Into<String>, S: Into<String>>(
message: M,
source: S,
) -> Self {
AixError::Other {
message: message.into(),
source: Some(source.into()),
}
}
}
impl fmt::Display for AixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AixError::Transport { source, context } => {
write!(f, "Transport error in {}: {}", context, source)
}
AixError::Provider {
provider,
code,
message,
status,
} => {
write!(f, "Provider error from {}", provider)?;
if let Some(status) = status {
write!(f, " (status {})", status)?;
}
if let Some(code) = code {
write!(f, " (code: {})", code)?;
}
write!(f, ": {}", message)
}
AixError::RateLimit {
provider,
retry_after,
message,
} => {
write!(f, "Rate limit error from {}: {}", provider, message)?;
if let Some(retry_after) = retry_after {
write!(f, " (retry after: {:?})", retry_after)?;
}
Ok(())
}
AixError::Serialization { source, context } => {
write!(f, "Serialization error in {}: {}", context, source)
}
AixError::Config { message } => {
write!(f, "Configuration error: {}", message)
}
AixError::Stream { message, source } => {
write!(f, "Stream error: {}", message)?;
if let Some(source) = source {
write!(f, " (source: {})", source)?;
}
Ok(())
}
AixError::Safety {
provider,
category,
message,
} => {
write!(
f,
"Safety violation from {} (category: {}): {}",
provider, category, message
)
}
AixError::Auth { provider, message } => {
write!(f, "Authentication error from {}: {}", provider, message)
}
AixError::Timeout { operation, duration } => {
write!(
f,
"Operation '{}' timed out after {:?}",
operation, duration
)
}
AixError::Other { message, source } => {
write!(f, "Error: {}", message)?;
if let Some(source) = source {
write!(f, " (source: {})", source)?;
}
Ok(())
}
}
}
}
impl StdError for AixError {}
/// Result type alias for AIX operations.
pub type AixResult<T> = Result<T, AixError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_retryability() {
assert!(AixError::transport("network error", "request").is_retryable());
assert!(AixError::provider_with_details("openai", "error", 500, "internal_error").is_retryable());
assert!(AixError::provider_with_details("openai", "error", 429, "rate_limit").is_retryable());
assert!(AixError::rate_limit("openai", "too many requests").is_retryable());
assert!(AixError::timeout("chat", Duration::from_secs(30)).is_retryable());
assert!(!AixError::provider_with_details("openai", "error", 400, "bad_request").is_retryable());
assert!(!AixError::config("invalid api key").is_retryable());
assert!(!AixError::auth("openai", "unauthorized").is_retryable());
assert!(!AixError::safety("openai", "hate", "content flagged").is_retryable());
}
#[test]
fn test_error_display() {
let err = AixError::provider_with_details("openai", "invalid request", 400, "invalid_request");
assert_eq!(err.to_string(), "Provider error from openai (status 400) (code: invalid_request): invalid request");
}
}