Skip to content

Commit 407eb4c

Browse files
committed
Update 0.9.3
1 parent 2fb12e3 commit 407eb4c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub mod context;
6565
pub mod registry;
6666
pub mod collector;
6767
pub mod logging;
68+
pub mod recovery;
6869

6970
#[cfg(feature = "async")]
7071
pub mod async_error;

src/recovery/forge_extensions.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::error::ForgeError;
2+
use crate::recovery::{RetryPolicy, CircuitBreaker};
3+
4+
/// Extension trait that adds recovery capabilities to ForgeError types
5+
pub trait ForgeErrorRecovery: ForgeError {
6+
/// Create a retry policy optimized for this error type
7+
fn create_retry_policy(&self, max_retries: usize) -> RetryPolicy {
8+
let policy = RetryPolicy::new_exponential()
9+
.with_max_retries(max_retries);
10+
policy
11+
}
12+
13+
/// Execute a fallible operation with retries if this error type is retryable
14+
fn retry<F, T, E>(&self, max_retries: usize, operation: F) -> Result<T, E>
15+
where
16+
F: FnMut() -> Result<T, E>,
17+
E: ForgeError,
18+
{
19+
let policy = self.create_retry_policy(max_retries);
20+
policy.forge_executor().retry(operation)
21+
}
22+
23+
/// Create a circuit breaker for operations that might result in this error type
24+
fn create_circuit_breaker(&self, name: impl Into<String>) -> CircuitBreaker {
25+
CircuitBreaker::new(name)
26+
}
27+
}
28+
29+
// Implement the extension trait for all ForgeError types
30+
impl<T: ForgeError> ForgeErrorRecovery for T {}

0 commit comments

Comments
 (0)