pub trait ErrorHandler<InError> {
type OutError;
// Required method
fn handle(
&mut self,
attempt: usize,
_: InError,
) -> RetryPolicy<Self::OutError>;
// Provided method
fn ok(&mut self, _attempt: usize) { ... }
}
Expand description
An error handler trait.
Please note that this trait is implemented for any FnMut
closure with a compatible signature,
so for some simple cases you might simply use a closure instead of creating your own type and
implementing this trait for it.
Here’s an example of an error handler that counts consecutive error attempts.
use futures_retry::{ErrorHandler, RetryPolicy};
use std::io;
use std::time::Duration;
pub struct CustomHandler {
max_attempts: usize,
}
impl CustomHandler {
pub fn new(attempts: usize) -> Self {
Self {
max_attempts: attempts,
}
}
}
impl ErrorHandler<io::Error> for CustomHandler {
type OutError = io::Error;
fn handle(&mut self, attempt: usize, e: io::Error) -> RetryPolicy<io::Error> {
if attempt == self.max_attempts {
eprintln!("No attempts left");
return RetryPolicy::ForwardError(e);
}
match e.kind() {
io::ErrorKind::ConnectionRefused => RetryPolicy::WaitRetry(Duration::from_secs(1)),
io::ErrorKind::TimedOut => RetryPolicy::Repeat,
_ => RetryPolicy::ForwardError(e),
}
}
}
Required Associated Types§
Required Methods§
Sourcefn handle(&mut self, attempt: usize, _: InError) -> RetryPolicy<Self::OutError>
fn handle(&mut self, attempt: usize, _: InError) -> RetryPolicy<Self::OutError>
Handles an error.
Refer to the RetryPolicy
type to understand what this method
might return.