RetryError

Struct RetryError 

Source
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>

Source

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.

Source

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()));
Source

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().

Source

pub fn sources(&self) -> impl Iterator<Item = &E>

Return an iterator over all of the reasons that the attempt behind this RetryError has failed.

Source

pub fn len(&self) -> usize

Return the number of underlying errors.

Source

pub fn is_empty(&self) -> bool

Return true if no underlying errors have been added.

Source

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);
Source

pub fn dedup_by<F>(&mut self, same_err: F)
where F: Fn(&E, &E) -> bool,

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.

Source

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

Source§

impl<E: PartialEq<E>> RetryError<E>

Source

pub fn dedup(&mut self)

Group up consecutive errors of the same kind, according to the PartialEq implementation.

Trait Implementations§

Source§

impl<E: Clone> Clone for RetryError<E>

Source§

fn clone(&self) -> RetryError<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug> Debug for RetryError<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E: AsRef<dyn Error>> Display for RetryError<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>

Formats the value using the given formatter. Read more
Source§

impl<E: Debug + AsRef<dyn Error>> Error for RetryError<E>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<E> IntoIterator for RetryError<E>

Source§

type Item = E

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<E>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<E> Freeze for RetryError<E>

§

impl<E> RefUnwindSafe for RetryError<E>
where E: RefUnwindSafe,

§

impl<E> Send for RetryError<E>
where E: Send,

§

impl<E> Sync for RetryError<E>
where E: Sync,

§

impl<E> Unpin for RetryError<E>
where E: Unpin,

§

impl<E> UnwindSafe for RetryError<E>
where E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.