pub struct RetryError<E> { /* private fields */ }Expand description
An error type for use when we’re going to do something a few times, and they might all fail.
To use this error type, initialize a new RetryError before you
start trying to do whatever it is. Then, every time the operation
fails, use RetryError::push() to add a new error to the list
of errors. If the operation fails too many times, you can use
RetryError as an Error itself.
This type now tracks timestamps for each error occurrence, allowing users to see when errors occurred and how long the retry process took.
Implementations§
Source§impl<E> RetryError<E>
impl<E> RetryError<E>
Sourcepub fn in_attempt_to<T: Into<String>>(doing: T) -> Self
pub fn in_attempt_to<T: Into<String>>(doing: T) -> Self
Create a new RetryError, with no failed attempts.
The provided doing argument is a short string that describes
what we were trying to do when we failed too many times. It
will be used to format the final error message; it should be a
phrase that can go after “while trying to”.
This RetryError should not be used as-is, since when no
Errors have been pushed into it, it doesn’t represent an
actual failure.
Sourcepub fn push_timed<T>(
&mut self,
err: T,
instant: Instant,
wall_clock: Option<SystemTime>,
)where
T: Into<E>,
pub fn push_timed<T>(
&mut self,
err: T,
instant: Instant,
wall_clock: Option<SystemTime>,
)where
T: Into<E>,
Add an error to this RetryError with explicit timestamps.
You should call this method when an attempt at the underlying operation has failed.
The instant parameter should be the monotonic time when the error
occurred, typically obtained from a runtime’s now() method.
The wall_clock parameter is the wall-clock time when the error occurred,
used for human-readable display. Pass None to skip wall-clock tracking,
or Some(SystemTime::now()) for the current time.
§Example
let mut retry_err: RetryError<&str> = RetryError::in_attempt_to("connect");
let now = Instant::now();
retry_err.push_timed("connection failed", now, Some(SystemTime::now()));Sourcepub fn push<T>(&mut self, err: T)where
T: Into<E>,
pub fn push<T>(&mut self, err: T)where
T: Into<E>,
Add an error to this RetryError using the current time.
You should call this method when an attempt at the underlying operation has failed.
This is a convenience wrapper around push_timed()
that uses Instant::now() and SystemTime::now() for the timestamps.
For code that needs mockable time (such as in tests), prefer push_timed().
Sourcepub fn sources(&self) -> impl Iterator<Item = &E>
pub fn sources(&self) -> impl Iterator<Item = &E>
Return an iterator over all of the reasons that the attempt behind this RetryError has failed.
Sourcepub fn extend<T>(&mut self, iter: impl IntoIterator<Item = T>)where
T: Into<E>,
pub fn extend<T>(&mut self, iter: impl IntoIterator<Item = T>)where
T: Into<E>,
Add multiple errors to this RetryError using the current time.
This method uses push() internally, which captures
SystemTime::now(). For code that needs mockable time (such as in tests),
iterate manually and call push_timed() instead.
§Example
let mut err: RetryError<anyhow::Error> = RetryError::in_attempt_to("parse");
let errors = vec!["error1", "error2"].into_iter().map(anyhow::Error::msg);
err.extend(errors);Sourcepub fn dedup_by<F>(&mut self, same_err: F)
pub fn dedup_by<F>(&mut self, same_err: F)
Group up consecutive errors of the same kind, for easier display.
Two errors have “the same kind” if they return true when passed
to the provided dedup function.
Sourcepub fn extend_from_retry_error(&mut self, other: RetryError<E>)
pub fn extend_from_retry_error(&mut self, other: RetryError<E>)
Add multiple errors to this RetryError, preserving their original timestamps.
The errors from other will be added to this RetryError, with their original
timestamps retained. The Attempt counters will be updated to continue from
the current state of this RetryError. Attempt::Range entries are preserved as ranges
Trait Implementations§
Source§impl<E: Clone> Clone for RetryError<E>
impl<E: Clone> Clone for RetryError<E>
Source§fn clone(&self) -> RetryError<E>
fn clone(&self) -> RetryError<E>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more