Skip to main content

ForgeErrorRecovery

Trait ForgeErrorRecovery 

Source
pub trait ForgeErrorRecovery: ForgeError {
    // Provided methods
    fn create_retry_policy(&self, max_retries: usize) -> RetryPolicy { ... }
    fn retry<F, T, E>(&self, max_retries: usize, operation: F) -> Result<T, E>
       where F: FnMut() -> Result<T, E>,
             E: ForgeError { ... }
    fn create_circuit_breaker(&self, name: impl Into<String>) -> CircuitBreaker { ... }
}
Expand description

Extension trait that adds recovery capabilities to ForgeError types

Provided Methods§

Source

fn create_retry_policy(&self, max_retries: usize) -> RetryPolicy

Create a retry policy optimized for this error type

Source

fn retry<F, T, E>(&self, max_retries: usize, operation: F) -> Result<T, E>
where F: FnMut() -> Result<T, E>, E: ForgeError,

Execute a fallible operation with retries if this error type is retryable.

When self.is_retryable() is false the operation runs exactly once and its result is returned as is. Otherwise it is retried up to max_retries times with exponential backoff, for as long as the error it returns is itself retryable.

§Example
use error_forge::{recovery::ForgeErrorRecovery, AppError};

// `AppError::config` is not retryable, so the operation runs once.
let template = AppError::config("bad config");
let mut calls = 0;
let result: Result<(), AppError> = template.retry(3, || {
    calls += 1;
    Err(AppError::network("db", None))
});
assert!(result.is_err());
assert_eq!(calls, 1);
Source

fn create_circuit_breaker(&self, name: impl Into<String>) -> CircuitBreaker

Create a circuit breaker for operations that might result in this error type

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§