pub enum RetryError {
MaxAttemptsExceeded {
attempts: u32,
max_attempts: u32,
},
MaxDurationExceeded {
duration: Duration,
max_duration: Duration,
},
OperationTimeout {
duration: Duration,
timeout: Duration,
},
Aborted {
reason: String,
},
ConfigError {
message: String,
},
DelayStrategyError {
message: String,
},
ExecutionError {
source: Box<dyn Error + Send + Sync>,
},
Other {
message: String,
},
}Expand description
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
use prism3_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
Variants§
MaxAttemptsExceeded
Maximum attempts exceeded
Triggered when the number of retries reaches or exceeds the preset maximum retry count.
§Fields
attempts- Actual number of attemptsmax_attempts- Maximum allowed retry count
MaxDurationExceeded
Maximum duration exceeded
Triggered when the total duration of retry operations exceeds the preset maximum duration.
§Fields
duration- Actual time consumedmax_duration- Maximum allowed duration
OperationTimeout
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 timetimeout- Configured timeout duration
Aborted
Operation aborted
Triggered when retry operation is aborted by external factors (e.g., user cancellation, system signals).
§Fields
reason- Description of the abort reason
ConfigError
Configuration error
Triggered when retry configuration parameters are invalid or conflicting.
§Fields
message- Error description message
DelayStrategyError
Delay strategy error
Triggered when delay strategy configuration is erroneous or an error occurs while calculating delays.
§Fields
message- Error description message
ExecutionError
Execution error
Wraps the original error when the retried operation itself fails.
§Fields
source- Original error, supports error chain tracking
Other
Other errors
Used to represent other error situations that don’t fall into the above categories.
§Fields
message- Error description message
Implementations§
Source§impl RetryError
impl RetryError
Sourcepub fn max_attempts_exceeded(attempts: u32, max_attempts: u32) -> Self
pub fn max_attempts_exceeded(attempts: u32, max_attempts: u32) -> Self
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 attemptsmax_attempts- Maximum allowed retry count
§Returns
Returns a RetryError containing retry count information
§Example
use prism3_retry::RetryError;
let error = RetryError::max_attempts_exceeded(5, 3);
assert!(error.to_string().contains("Maximum attempts exceeded"));Sourcepub fn max_duration_exceeded(duration: Duration, max_duration: Duration) -> Self
pub fn max_duration_exceeded(duration: Duration, max_duration: Duration) -> Self
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 consumedmax_duration- Maximum allowed duration
§Returns
Returns a RetryError containing time information
§Example
use prism3_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"));Sourcepub fn operation_timeout(duration: Duration, timeout: Duration) -> Self
pub fn operation_timeout(duration: Duration, timeout: Duration) -> Self
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 timetimeout- Configured timeout duration
§Returns
Returns a RetryError containing time information
§Example
use prism3_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"));Sourcepub fn aborted(reason: &str) -> Self
pub fn aborted(reason: &str) -> Self
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
use prism3_retry::RetryError;
let error = RetryError::aborted("User cancelled operation");
assert!(error.to_string().contains("Operation aborted"));Sourcepub fn config_error(message: &str) -> Self
pub fn config_error(message: &str) -> Self
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
use prism3_retry::RetryError;
let error = RetryError::config_error("Maximum retry count cannot be negative");
assert!(error.to_string().contains("Configuration error"));Sourcepub fn delay_strategy_error(message: &str) -> Self
pub fn delay_strategy_error(message: &str) -> Self
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
use prism3_retry::RetryError;
let error = RetryError::delay_strategy_error("Delay time calculation overflow");
assert!(error.to_string().contains("Delay strategy error"));Sourcepub fn execution_error<E: Error + Send + Sync + 'static>(error: E) -> Self
pub fn execution_error<E: Error + Send + Sync + 'static>(error: E) -> Self
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
use prism3_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"));Sourcepub fn execution_error_box(error: Box<dyn Error + Send + Sync>) -> Self
pub fn execution_error_box(error: Box<dyn Error + Send + Sync>) -> Self
Create execution error (from Box
When you already have a Box
§Parameters
error- Boxed error
§Returns
Returns a RetryError wrapping the original error
§Example
use prism3_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"));Sourcepub fn other(message: &str) -> Self
pub fn other(message: &str) -> Self
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
use prism3_retry::RetryError;
let error = RetryError::other("Unknown error type");
assert!(error.to_string().contains("Other error"));Trait Implementations§
Source§impl Debug for RetryError
impl Debug for RetryError
Source§impl Display for RetryError
impl Display for RetryError
Source§impl Error for RetryError
impl Error for RetryError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
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
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<Box<dyn Error + Send + Sync>> for RetryError
Convert from boxed error types
impl From<Box<dyn Error + Send + Sync>> for RetryError
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
use prism3_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();Source§impl From<Error> for RetryError
Convert from standard error types
impl From<Error> for 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
use prism3_retry::{RetryError, RetryResult};
fn io_operation() -> RetryResult<()> {
let file = std::fs::File::open("nonexistent_file.txt")?;
// Do something with file
Ok(())
}