qubit-retry 0.2.0

Retry module, providing a feature-complete, type-safe retry management system with support for multiple delay strategies and event listeners
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026.
 *    Haixing Hu, Qubit Co. Ltd.
 *
 *    All rights reserved.
 *
 ******************************************************************************/
//! # Retry Error Types
//!
//! Defines error types used in the retry module.
//!
//! # Author
//!
//! Haixing Hu

use std::error::Error;
use std::fmt;

/// Error type for the retry module
///
/// Defines various error conditions that can occur during retry operations, including exceeding maximum retries,
/// exceeding duration limits, operation abortion, configuration errors, etc.
///
/// # Features
///
/// - Supports unified handling of multiple error types
/// - Provides detailed error information and context
/// - Supports error chain tracking (via the source method)
/// - Implements the standard Error trait for interoperability with other error types
///
/// # Use Cases
///
/// Suitable for operations requiring retry mechanisms, such as network requests, file operations, database connections, etc.
/// Returns a corresponding RetryError when retry strategies fail or encounter unrecoverable errors.
///
/// # Example
///
/// ```rust
/// use qubit_retry::RetryError;
///
/// // Create maximum attempts exceeded error
/// let error = RetryError::max_attempts_exceeded(5, 3);
/// println!("Error: {}", error);
///
/// // Create execution error
/// let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
/// let retry_error = RetryError::execution_error(io_error);
/// println!("Execution error: {}", retry_error);
/// ```
///
/// # Author
///
/// Haixing Hu
///
#[derive(Debug)]
pub enum RetryError {
    /// Maximum attempts exceeded
    ///
    /// Triggered when the number of retries reaches or exceeds the preset maximum retry count.
    ///
    /// # Fields
    ///
    /// * `attempts` - Actual number of attempts
    /// * `max_attempts` - Maximum allowed retry count
    MaxAttemptsExceeded { attempts: u32, max_attempts: u32 },

    /// Maximum duration exceeded
    ///
    /// Triggered when the total duration of retry operations exceeds the preset maximum duration.
    ///
    /// # Fields
    ///
    /// * `duration` - Actual time consumed
    /// * `max_duration` - Maximum allowed duration
    ///
    MaxDurationExceeded {
        duration: std::time::Duration,
        max_duration: std::time::Duration,
    },

    /// Single operation timeout
    ///
    /// Triggered when the execution time of a single operation exceeds the configured operation timeout.
    /// This differs from MaxDurationExceeded, which is for the total time limit of the entire retry process.
    ///
    /// # Fields
    ///
    /// * `duration` - Actual execution time
    /// * `timeout` - Configured timeout duration
    ///
    OperationTimeout {
        duration: std::time::Duration,
        timeout: std::time::Duration,
    },

    /// Operation aborted
    ///
    /// Triggered when retry operation is aborted by external factors (e.g., user cancellation, system signals).
    ///
    /// # Fields
    ///
    /// * `reason` - Description of the abort reason
    ///
    Aborted { reason: String },

    /// Configuration error
    ///
    /// Triggered when retry configuration parameters are invalid or conflicting.
    ///
    /// # Fields
    ///
    /// * `message` - Error description message
    ///
    ConfigError { message: String },

    /// Delay strategy error
    ///
    /// Triggered when delay strategy configuration is erroneous or an error occurs while calculating delays.
    ///
    /// # Fields
    ///
    /// * `message` - Error description message
    ///
    DelayStrategyError { message: String },

    /// Execution error
    ///
    /// Wraps the original error when the retried operation itself fails.
    ///
    /// # Fields
    ///
    /// * `source` - Original error, supports error chain tracking
    ///
    ExecutionError {
        source: Box<dyn Error + Send + Sync>,
    },

    /// Other errors
    ///
    /// Used to represent other error situations that don't fall into the above categories.
    ///
    /// # Fields
    ///
    /// * `message` - Error description message
    ///
    Other { message: String },
}

impl fmt::Display for RetryError {
    /// Format error information into a readable string
    ///
    /// Provides error descriptions for each error type, including relevant contextual information.
    ///
    /// # Parameters
    ///
    /// * `f` - Formatter
    ///
    /// # Returns
    ///
    /// Returns formatting result
    ///
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RetryError::MaxAttemptsExceeded {
                attempts,
                max_attempts,
            } => {
                write!(
                    f,
                    "Maximum attempts exceeded: {} (max: {})",
                    attempts, max_attempts
                )
            }
            RetryError::MaxDurationExceeded {
                duration,
                max_duration,
            } => {
                write!(
                    f,
                    "Maximum duration exceeded: {:?} (max: {:?})",
                    duration, max_duration
                )
            }
            RetryError::OperationTimeout { duration, timeout } => {
                write!(
                    f,
                    "Operation timeout: execution time {:?} exceeded configured timeout {:?}",
                    duration, timeout
                )
            }
            RetryError::Aborted { reason } => {
                write!(f, "Operation aborted: {}", reason)
            }
            RetryError::ConfigError { message } => {
                write!(f, "Configuration error: {}", message)
            }
            RetryError::DelayStrategyError { message } => {
                write!(f, "Delay strategy error: {}", message)
            }
            RetryError::ExecutionError { source } => {
                write!(f, "Execution error: {}", source)
            }
            RetryError::Other { message } => {
                write!(f, "Other error: {}", message)
            }
        }
    }
}

impl Error for RetryError {
    /// Get the root cause of the error
    ///
    /// For ExecutionError type, returns the original error; for other types, returns None.
    /// This supports error chain tracking, aiding in debugging and error handling.
    ///
    /// # Returns
    ///
    /// Returns the root cause of the error, or None if it doesn't exist
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            RetryError::ExecutionError { source } => Some(source.as_ref()),
            _ => None,
        }
    }
}

impl RetryError {
    /// Create maximum attempts exceeded error
    ///
    /// Use this method to create an error when the retry count reaches or exceeds the preset maximum retry count.
    ///
    /// # Parameters
    ///
    /// * `attempts` - Actual number of attempts
    /// * `max_attempts` - Maximum allowed retry count
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing retry count information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let error = RetryError::max_attempts_exceeded(5, 3);
    /// assert!(error.to_string().contains("Maximum attempts exceeded"));
    /// ```
    pub fn max_attempts_exceeded(attempts: u32, max_attempts: u32) -> Self {
        RetryError::MaxAttemptsExceeded {
            attempts,
            max_attempts,
        }
    }

    /// Create maximum duration exceeded error
    ///
    /// Use this method to create an error when the total duration of retry operations exceeds the preset maximum duration.
    ///
    /// # Parameters
    ///
    /// * `duration` - Actual time consumed
    /// * `max_duration` - Maximum allowed duration
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing time information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    /// use std::time::Duration;
    ///
    /// let error = RetryError::max_duration_exceeded(
    ///     Duration::from_secs(10),
    ///     Duration::from_secs(5)
    /// );
    /// assert!(error.to_string().contains("Maximum duration exceeded"));
    /// ```
    pub fn max_duration_exceeded(
        duration: std::time::Duration,
        max_duration: std::time::Duration,
    ) -> Self {
        RetryError::MaxDurationExceeded {
            duration,
            max_duration,
        }
    }

    /// Create single operation timeout error
    ///
    /// Use this method to create an error when the execution time of a single operation exceeds the configured operation timeout.
    ///
    /// # Parameters
    ///
    /// * `duration` - Actual execution time
    /// * `timeout` - Configured timeout duration
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing time information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    /// use std::time::Duration;
    ///
    /// let error = RetryError::operation_timeout(
    ///     Duration::from_secs(10),
    ///     Duration::from_secs(5)
    /// );
    /// assert!(error.to_string().contains("Operation timeout"));
    /// ```
    pub fn operation_timeout(duration: std::time::Duration, timeout: std::time::Duration) -> Self {
        RetryError::OperationTimeout { duration, timeout }
    }

    /// Create abort error
    ///
    /// Use this method to create an error when the retry operation is aborted by external factors.
    ///
    /// # Parameters
    ///
    /// * `reason` - Reason for abortion
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing the abort reason
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let error = RetryError::aborted("User cancelled operation");
    /// assert!(error.to_string().contains("Operation aborted"));
    /// ```
    pub fn aborted(reason: &str) -> Self {
        RetryError::Aborted {
            reason: reason.to_string(),
        }
    }

    /// Create configuration error
    ///
    /// Use this method to create an error when retry configuration parameters are invalid or conflicting.
    ///
    /// # Parameters
    ///
    /// * `message` - Error description message
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing error information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let error = RetryError::config_error("Maximum retry count cannot be negative");
    /// assert!(error.to_string().contains("Configuration error"));
    /// ```
    pub fn config_error(message: &str) -> Self {
        RetryError::ConfigError {
            message: message.to_string(),
        }
    }

    /// Create delay strategy error
    ///
    /// Use this method to create an error when delay strategy configuration is erroneous or an error occurs while calculating delays.
    ///
    /// # Parameters
    ///
    /// * `message` - Error description message
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing error information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let error = RetryError::delay_strategy_error("Delay time calculation overflow");
    /// assert!(error.to_string().contains("Delay strategy error"));
    /// ```
    pub fn delay_strategy_error(message: &str) -> Self {
        RetryError::DelayStrategyError {
            message: message.to_string(),
        }
    }

    /// Create execution error
    ///
    /// Use this method to wrap the original error as RetryError when the retried operation itself fails.
    ///
    /// # Parameters
    ///
    /// * `error` - Original error, must implement Error + Send + Sync trait
    ///
    /// # Returns
    ///
    /// Returns a RetryError wrapping the original error
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
    /// let retry_error = RetryError::execution_error(io_error);
    /// assert!(retry_error.to_string().contains("Execution error"));
    /// ```
    pub fn execution_error<E: Error + Send + Sync + 'static>(error: E) -> Self {
        RetryError::ExecutionError {
            source: Box::new(error),
        }
    }

    /// Create execution error (from Box<dyn Error>)
    ///
    /// When you already have a Box<dyn Error>, use this method to create an execution error directly.
    /// This avoids additional boxing operations.
    ///
    /// # Parameters
    ///
    /// * `error` - Boxed error
    ///
    /// # Returns
    ///
    /// Returns a RetryError wrapping the original error
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
    /// let boxed_error = Box::new(io_error);
    /// let retry_error = RetryError::execution_error_box(boxed_error);
    /// assert!(retry_error.to_string().contains("Execution error"));
    /// ```
    pub fn execution_error_box(error: Box<dyn Error + Send + Sync>) -> Self {
        RetryError::ExecutionError { source: error }
    }

    /// Create other error
    ///
    /// Use this method to create an error for other error situations that don't fall into the above categories.
    ///
    /// # Parameters
    ///
    /// * `message` - Error description message
    ///
    /// # Returns
    ///
    /// Returns a RetryError containing error information
    ///
    /// # Example
    ///
    /// ```rust
    /// use qubit_retry::RetryError;
    ///
    /// let error = RetryError::other("Unknown error type");
    /// assert!(error.to_string().contains("Other error"));
    /// ```
    pub fn other(message: &str) -> Self {
        RetryError::Other {
            message: message.to_string(),
        }
    }
}

/// Retry result type alias
///
/// Represents the result of a retry operation, returning type T on success and RetryError on failure.
/// This is the unified return type for all operations in the retry module.
///
/// # Type Parameters
///
/// * `T` - The data type returned on success
///
/// # Example
///
/// ```rust
/// use qubit_retry::{RetryResult, RetryError};
///
/// fn retry_operation() -> RetryResult<String> {
///     // Simulate retry operation
///     Ok("Operation successful".to_string())
/// }
///
/// fn retry_operation_failed() -> RetryResult<String> {
///     Err(RetryError::other("Operation failed"))
/// }
/// ```
pub type RetryResult<T> = Result<T, RetryError>;

/// Convert from standard error types
///
/// Provides automatic conversion from std::io::Error to RetryError.
/// This simplifies error handling, allowing direct use of the ? operator.
///
/// # Parameters
///
/// * `error` - IO error
///
/// # Returns
///
/// Returns a RetryError wrapping the IO error
///
/// # Example
///
/// ```rust
/// use qubit_retry::{RetryError, RetryResult};
///
/// fn io_operation() -> RetryResult<()> {
///     let file = std::fs::File::open("nonexistent_file.txt")?;
///     // Do something with file
///     Ok(())
/// }
/// ```
impl From<std::io::Error> for RetryError {
    /// Convert std::io::Error to RetryError
    fn from(error: std::io::Error) -> Self {
        RetryError::ExecutionError {
            source: Box::new(error),
        }
    }
}

/// Convert from boxed error types
///
/// Provides automatic conversion from Box<dyn Error + Send + Sync> to RetryError.
/// This allows converting any boxed error directly to RetryError.
///
/// # Parameters
///
/// * `error` - Boxed error
///
/// # Returns
///
/// Returns a RetryError wrapping the original error
///
/// # Example
///
/// ```rust
/// use qubit_retry::RetryError;
///
/// let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
/// let boxed_error: Box<dyn std::error::Error + Send + Sync> = Box::new(io_error);
/// let retry_error: RetryError = boxed_error.into();
/// ```
impl From<Box<dyn Error + Send + Sync>> for RetryError {
    /// Convert boxed error to RetryError
    fn from(error: Box<dyn Error + Send + Sync>) -> Self {
        RetryError::ExecutionError { source: error }
    }
}