Skip to main content

retry_error/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] // See arti#2556
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40#![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44#![allow(clippy::needless_lifetimes)] // See arti#1765
45#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46#![allow(clippy::collapsible_if)] // See arti#2342
47#![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] // See arti#2571
49#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51
52// TODO: add to the list maintained by maint/add_warning once it's available on all crates
53#![warn(unnameable_types)]
54
55use std::error::Error;
56use std::fmt::{self, Debug, Display, Error as FmtError, Formatter};
57use std::iter;
58use std::time::{Duration, SystemTime};
59
60#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
61use web_time::Instant;
62
63#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
64use std::time::Instant;
65
66/// An error type for use when we're going to do something a few times,
67/// and they might all fail.
68///
69/// To use this error type, initialize a new RetryError before you
70/// start trying to do whatever it is.  Then, every time the operation
71/// fails, use [`RetryError::push()`] to add a new error to the list
72/// of errors.  If the operation fails too many times, you can use
73/// RetryError as an [`Error`] itself.
74///
75/// This type now tracks timestamps for each error occurrence, allowing
76/// users to see when errors occurred and how long the retry process took.
77#[derive(Debug, Clone)]
78pub struct RetryError<E> {
79    /// The operation we were trying to do.
80    doing: String,
81    /// The errors that we encountered when doing the operation.
82    errors: Vec<(Attempt, E, Instant)>,
83    /// The total number of errors we encountered.
84    ///
85    /// This can differ from errors.len() if the errors have been
86    /// deduplicated.
87    n_errors: usize,
88    /// The wall-clock time when the first error occurred.
89    ///
90    /// This is used for human-readable display of absolute timestamps.
91    ///
92    /// We store both types because they serve different purposes:
93    /// - `Instant` (in the errors vec): Monotonic clock for reliable duration calculations.
94    ///   Immune to clock adjustments, but can't be displayed as wall-clock time.
95    /// - `SystemTime` (here): Wall-clock time for displaying when the first error occurred
96    ///   in a human-readable format (e.g., "2025-12-09T10:24:02Z").
97    ///
98    /// We only store `SystemTime` for the first error to show users *when* the problem
99    /// started. Subsequent errors are displayed relative to the first ("+2m 30s"),
100    /// using the reliable `Instant` timestamps.
101    first_error_at: Option<SystemTime>,
102}
103
104/// Represents which attempts, in sequence, failed to complete.
105#[derive(Debug, Clone)]
106enum Attempt {
107    /// A single attempt that failed.
108    Single(usize),
109    /// A range of consecutive attempts that failed.
110    Range(usize, usize),
111}
112
113// TODO: Should we declare that some error is the 'source' of this one?
114// If so, should it be the first failure?  The last?
115impl<E: Debug + AsRef<dyn Error>> Error for RetryError<E> {}
116
117impl<E> RetryError<E> {
118    /// Create a new RetryError, with no failed attempts.
119    ///
120    /// The provided `doing` argument is a short string that describes
121    /// what we were trying to do when we failed too many times.  It
122    /// will be used to format the final error message; it should be a
123    /// phrase that can go after "while trying to".
124    ///
125    /// This RetryError should not be used as-is, since when no
126    /// [`Error`]s have been pushed into it, it doesn't represent an
127    /// actual failure.
128    pub fn in_attempt_to<T: Into<String>>(doing: T) -> Self {
129        RetryError {
130            doing: doing.into(),
131            errors: Vec::new(),
132            n_errors: 0,
133            first_error_at: None,
134        }
135    }
136    /// Add an error to this RetryError with explicit timestamps.
137    ///
138    /// You should call this method when an attempt at the underlying operation
139    /// has failed.
140    ///
141    /// The `instant` parameter should be the monotonic time when the error
142    /// occurred, typically obtained from a runtime's `now()` method.
143    ///
144    /// The `wall_clock` parameter is the wall-clock time when the error occurred,
145    /// used for human-readable display. Pass `None` to skip wall-clock tracking,
146    /// or `Some(SystemTime::now())` for the current time.
147    ///
148    /// # Example
149    /// ```
150    /// # #![allow(clippy::disallowed_methods)]
151    /// # use retry_error::RetryError;
152    /// # use std::time::{Instant, SystemTime};
153    /// let mut retry_err: RetryError<&str> = RetryError::in_attempt_to("connect");
154    /// let now = Instant::now();
155    /// retry_err.push_timed("connection failed", now, Some(SystemTime::now()));
156    /// ```
157    pub fn push_timed<T>(&mut self, err: T, instant: Instant, wall_clock: Option<SystemTime>)
158    where
159        T: Into<E>,
160    {
161        if self.n_errors < usize::MAX {
162            self.n_errors += 1;
163            let attempt = Attempt::Single(self.n_errors);
164
165            if self.first_error_at.is_none() {
166                self.first_error_at = wall_clock;
167            }
168
169            self.errors.push((attempt, err.into(), instant));
170        }
171    }
172
173    /// Add an error to this RetryError using the current time.
174    ///
175    /// You should call this method when an attempt at the underlying operation
176    /// has failed.
177    ///
178    /// This is a convenience wrapper around [`push_timed()`](Self::push_timed)
179    /// that uses `Instant::now()` and `SystemTime::now()` for the timestamps.
180    /// For code that needs mockable time (such as in tests), prefer `push_timed()`.
181    pub fn push<T>(&mut self, err: T)
182    where
183        T: Into<E>,
184    {
185        self.push_timed(err, current_instant(), Some(current_system_time()));
186    }
187
188    /// Return an iterator over all of the reasons that the attempt
189    /// behind this RetryError has failed.
190    pub fn sources(&self) -> impl Iterator<Item = &E> {
191        self.errors.iter().map(|(.., e, _)| e)
192    }
193
194    /// Return the number of underlying errors.
195    pub fn len(&self) -> usize {
196        self.errors.len()
197    }
198
199    /// Return true if no underlying errors have been added.
200    pub fn is_empty(&self) -> bool {
201        self.errors.is_empty()
202    }
203
204    /// Add multiple errors to this RetryError using the current time.
205    ///
206    /// This method uses [`push()`](Self::push) internally, which captures
207    /// `SystemTime::now()`. For code that needs mockable time (such as in tests),
208    /// iterate manually and call [`push_timed()`](Self::push_timed) instead.
209    ///
210    /// # Example
211    /// ```
212    /// # use retry_error::RetryError;
213    /// let mut err: RetryError<anyhow::Error> = RetryError::in_attempt_to("parse");
214    /// let errors = vec!["error1", "error2"].into_iter().map(anyhow::Error::msg);
215    /// err.extend(errors);
216    /// ```
217    #[allow(clippy::disallowed_methods)] // This method intentionally uses push()
218    pub fn extend<T>(&mut self, iter: impl IntoIterator<Item = T>)
219    where
220        T: Into<E>,
221    {
222        for item in iter {
223            self.push(item);
224        }
225    }
226
227    /// Group up consecutive errors of the same kind, for easier display.
228    ///
229    /// Two errors have "the same kind" if they return `true` when passed
230    /// to the provided `same_err` function.
231    pub fn dedup_by<F>(&mut self, same_err: F)
232    where
233        F: Fn(&E, &E) -> bool,
234    {
235        let mut old_errs = Vec::new();
236        std::mem::swap(&mut old_errs, &mut self.errors);
237
238        for (attempt, err, timestamp) in old_errs {
239            if let Some((last_attempt, last_err, ..)) = self.errors.last_mut() {
240                if same_err(last_err, &err) {
241                    last_attempt.grow(attempt.count());
242                } else {
243                    self.errors.push((attempt, err, timestamp));
244                }
245            } else {
246                self.errors.push((attempt, err, timestamp));
247            }
248        }
249    }
250
251    /// Add multiple errors to this RetryError, preserving their original timestamps.
252    ///
253    /// The errors from other will be added to this RetryError, with their original
254    /// timestamps retained. The `Attempt` counters will be updated to continue from
255    /// the current state of this RetryError. `Attempt::Range` entries are preserved as ranges
256    pub fn extend_from_retry_error(&mut self, other: RetryError<E>) {
257        if self.first_error_at.is_none() {
258            self.first_error_at = other.first_error_at;
259        }
260
261        for (attempt, err, timestamp) in other.errors {
262            let Some(new_n_errors) = self.n_errors.checked_add(attempt.count()) else {
263                break;
264            };
265
266            let new_attempt = match attempt {
267                Attempt::Single(_) => Attempt::Single(new_n_errors),
268                Attempt::Range(_, _) => Attempt::Range(self.n_errors + 1, new_n_errors),
269            };
270
271            self.errors.push((new_attempt, err, timestamp));
272            self.n_errors = new_n_errors;
273        }
274    }
275}
276
277impl<E: PartialEq<E>> RetryError<E> {
278    /// Group up consecutive errors of the same kind, according to the
279    /// `PartialEq` implementation.
280    pub fn dedup(&mut self) {
281        self.dedup_by(PartialEq::eq);
282    }
283}
284
285impl Attempt {
286    /// Extend this attempt by additional failures.
287    fn grow(&mut self, count: usize) {
288        *self = match *self {
289            Attempt::Single(idx) => Attempt::Range(idx, idx + count),
290            Attempt::Range(first, last) => Attempt::Range(first, last + count),
291        };
292    }
293
294    /// Return amount of failures.
295    fn count(&self) -> usize {
296        match *self {
297            Attempt::Single(_) => 1,
298            Attempt::Range(first, last) => last - first + 1,
299        }
300    }
301}
302
303impl<E> IntoIterator for RetryError<E> {
304    type Item = E;
305    type IntoIter = std::vec::IntoIter<E>;
306    #[allow(clippy::needless_collect)]
307    // TODO We have to use collect/into_iter here for now, since
308    // the actual Map<> type can't be named.  Once Rust lets us say
309    // `type IntoIter = impl Iterator<Item=E>` then we fix the code
310    // and turn the Clippy warning back on.
311    fn into_iter(self) -> Self::IntoIter {
312        self.errors
313            .into_iter()
314            .map(|(.., e, _)| e)
315            .collect::<Vec<_>>()
316            .into_iter()
317    }
318}
319
320impl Display for Attempt {
321    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
322        match self {
323            Attempt::Single(idx) => write!(f, "Attempt {}", idx),
324            Attempt::Range(first, last) => write!(f, "Attempts {}..{}", first, last),
325        }
326    }
327}
328
329impl<E: AsRef<dyn Error>> Display for RetryError<E> {
330    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
331        let show_timestamps = f.alternate();
332
333        match self.n_errors {
334            0 => write!(f, "Unable to {}. (No errors given)", self.doing),
335            1 => {
336                write!(f, "Unable to {}", self.doing)?;
337
338                if show_timestamps {
339                    if let (Some((.., timestamp)), Some(first_at)) =
340                        (self.errors.first(), self.first_error_at)
341                    {
342                        write!(
343                            f,
344                            " at {} ({})",
345                            humantime::format_rfc3339(first_at),
346                            FormatTimeAgo(timestamp.elapsed())
347                        )?;
348                    }
349                }
350
351                write!(f, ": ")?;
352                fmt_error_with_sources(self.errors[0].1.as_ref(), f)
353            }
354            n => {
355                write!(
356                    f,
357                    "Tried to {} {} times, but all attempts failed",
358                    self.doing, n
359                )?;
360
361                if show_timestamps {
362                    if let (Some(first_at), Some((.., first_ts)), Some((.., last_ts))) =
363                        (self.first_error_at, self.errors.first(), self.errors.last())
364                    {
365                        let duration = last_ts.saturating_duration_since(*first_ts);
366
367                        write!(f, " (from {} ", humantime::format_rfc3339(first_at))?;
368
369                        if duration.as_secs() > 0 {
370                            write!(f, "to {}", humantime::format_rfc3339(first_at + duration))?;
371                        }
372
373                        write!(f, ", {})", FormatTimeAgo(last_ts.elapsed()))?;
374                    }
375                }
376
377                let first_ts = self.errors.first().map(|(.., ts)| ts);
378                for (attempt, e, timestamp) in &self.errors {
379                    write!(f, "\n{}", attempt)?;
380
381                    if show_timestamps {
382                        if let Some(first_ts) = first_ts {
383                            let offset = timestamp.saturating_duration_since(*first_ts);
384                            if offset.as_secs() > 0 {
385                                write!(f, " (+{})", FormatDuration(offset))?;
386                            }
387                        }
388                    }
389
390                    write!(f, ": ")?;
391                    fmt_error_with_sources(e.as_ref(), f)?;
392                }
393                Ok(())
394            }
395        }
396    }
397}
398
399/// A wrapper for formatting a [`Duration`] in a human-readable way.
400/// Produces output like "2m 30s", "5h 12m", "45s", "500ms".
401///
402/// We use this instead of `humantime::format_duration` because humantime tends to produce overly verbose output.
403struct FormatDuration(Duration);
404
405impl Display for FormatDuration {
406    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
407        fmt_duration_impl(self.0, f)
408    }
409}
410
411/// A wrapper for formatting a [`Duration`] with "ago" suffix.
412struct FormatTimeAgo(Duration);
413
414impl Display for FormatTimeAgo {
415    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
416        let secs = self.0.as_secs();
417        let millis = self.0.as_millis();
418
419        // Special case: very recent times show as "just now" rather than "0s ago" or "0ms ago"
420        if secs == 0 && millis == 0 {
421            return write!(f, "just now");
422        }
423
424        fmt_duration_impl(self.0, f)?;
425        write!(f, " ago")
426    }
427}
428
429/// Internal helper to format a duration.
430///
431/// This function contains the actual formatting logic to avoid duplication
432/// between `FormatDuration` and `FormatTimeAgo`.
433fn fmt_duration_impl(duration: Duration, f: &mut Formatter<'_>) -> fmt::Result {
434    let secs = duration.as_secs();
435
436    if secs == 0 {
437        let millis = duration.as_millis();
438        if millis == 0 {
439            write!(f, "0s")
440        } else {
441            write!(f, "{}ms", millis)
442        }
443    } else if secs < 60 {
444        write!(f, "{}s", secs)
445    } else if secs < 3600 {
446        let mins = secs / 60;
447        let rem_secs = secs % 60;
448        if rem_secs == 0 {
449            write!(f, "{}m", mins)
450        } else {
451            write!(f, "{}m {}s", mins, rem_secs)
452        }
453    } else {
454        let hours = secs / 3600;
455        let mins = (secs % 3600) / 60;
456        if mins == 0 {
457            write!(f, "{}h", hours)
458        } else {
459            write!(f, "{}h {}m", hours, mins)
460        }
461    }
462}
463
464/// Helper: formats a [`std::error::Error`] and its sources (as `"error: source"`)
465///
466/// Avoids duplication in messages by not printing messages which are
467/// wholly-contained (textually) within already-printed messages.
468///
469/// Offered as a `fmt` function:
470/// this is for use in more-convenient higher-level error handling functionality,
471/// rather than directly in application/functional code.
472///
473/// This is used by `RetryError`'s impl of `Display`,
474/// but will be useful for other error-handling situations.
475///
476/// # Example
477///
478/// ```
479/// use std::fmt::{self, Display};
480///
481/// #[derive(Debug, thiserror::Error)]
482/// #[error("some pernickety problem")]
483/// struct Pernickety;
484///
485/// #[derive(Debug, thiserror::Error)]
486/// enum ApplicationError {
487///     #[error("everything is terrible")]
488///     Terrible(#[source] Pernickety),
489/// }
490///
491/// struct Wrapper(Box<dyn std::error::Error>);
492/// impl Display for Wrapper {
493///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
494///         retry_error::fmt_error_with_sources(&*self.0, f)
495///     }
496/// }
497///
498/// let bad = Pernickety;
499/// let err = ApplicationError::Terrible(bad);
500///
501/// let printed = Wrapper(err.into()).to_string();
502/// assert_eq!(printed, "everything is terrible: some pernickety problem");
503/// ```
504pub fn fmt_error_with_sources(mut e: &dyn Error, f: &mut fmt::Formatter) -> fmt::Result {
505    // We deduplicate the errors here under the assumption that the `Error` trait is poorly defined
506    // and contradictory, and that some error types will duplicate error messages. This is
507    // controversial, and since there isn't necessarily agreement, we should stick with the status
508    // quo here and avoid changing this behaviour without further discussion.
509    let mut last = String::new();
510    let mut sep = iter::once("").chain(iter::repeat(": "));
511
512    // Note that this loop does not use tor_basic_utils::ErrorSources.  We can't, because `e` is not
513    // `Error + 'static`.  But we shouldn't use ErrorSources here, since io::Error will format
514    // its inner by_ref() error, and so it's desirable that `source` skips over it.
515    loop {
516        let this = e.to_string();
517        if !last.contains(&this) {
518            write!(f, "{}{}", sep.next().expect("repeat ended"), this)?;
519        }
520        last = this;
521
522        if let Some(ne) = e.source() {
523            e = ne;
524        } else {
525            break;
526        }
527    }
528    Ok(())
529}
530
531/// Return the current system time.
532///
533/// (This is a separate method for compatibility with wasm32.)
534#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
535fn current_system_time() -> SystemTime {
536    use web_time::web::SystemTimeExt as _;
537    web_time::SystemTime::now().to_std()
538}
539
540/// Return the current system time.
541///
542/// (This is a separate method for compatibility with wasm32.)
543#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
544fn current_system_time() -> SystemTime {
545    #![allow(clippy::disallowed_methods)]
546    SystemTime::now()
547}
548
549/// Return the current Instant.
550///
551/// (This is a separate method for compatibility with wasm32.)
552fn current_instant() -> Instant {
553    #![allow(clippy::disallowed_methods)]
554    Instant::now()
555}
556
557#[cfg(test)]
558mod test {
559    // @@ begin test lint list maintained by maint/add_warning @@
560    #![allow(clippy::bool_assert_comparison)]
561    #![allow(clippy::clone_on_copy)]
562    #![allow(clippy::dbg_macro)]
563    #![allow(clippy::mixed_attributes_style)]
564    #![allow(clippy::print_stderr)]
565    #![allow(clippy::print_stdout)]
566    #![allow(clippy::single_char_pattern)]
567    #![allow(clippy::unwrap_used)]
568    #![allow(clippy::unchecked_time_subtraction)]
569    #![allow(clippy::useless_vec)]
570    #![allow(clippy::needless_pass_by_value)]
571    #![allow(clippy::string_slice)] // See arti#2571
572    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
573    #![allow(clippy::disallowed_methods)]
574    use super::*;
575    use derive_more::From;
576
577    #[test]
578    fn bad_parse1() {
579        let mut err: RetryError<anyhow::Error> = RetryError::in_attempt_to("convert some things");
580        if let Err(e) = "maybe".parse::<bool>() {
581            err.push(e);
582        }
583        if let Err(e) = "a few".parse::<u32>() {
584            err.push(e);
585        }
586        if let Err(e) = "the_g1b50n".parse::<std::net::IpAddr>() {
587            err.push(e);
588        }
589
590        let disp = format!("{}", err);
591        assert_eq!(
592            disp,
593            "\
594Tried to convert some things 3 times, but all attempts failed
595Attempt 1: provided string was not `true` or `false`
596Attempt 2: invalid digit found in string
597Attempt 3: invalid IP address syntax"
598        );
599
600        let disp_alt = format!("{:#}", err);
601        assert!(disp_alt.contains("Tried to convert some things 3 times, but all attempts failed"));
602        assert!(disp_alt.contains("(from 20")); // Year prefix for timestamp
603    }
604
605    #[test]
606    fn no_problems() {
607        let empty: RetryError<anyhow::Error> =
608            RetryError::in_attempt_to("immanentize the eschaton");
609        let disp = format!("{}", empty);
610        assert_eq!(
611            disp,
612            "Unable to immanentize the eschaton. (No errors given)"
613        );
614    }
615
616    #[test]
617    fn one_problem() {
618        let mut err: RetryError<anyhow::Error> =
619            RetryError::in_attempt_to("connect to torproject.org");
620        if let Err(e) = "the_g1b50n".parse::<std::net::IpAddr>() {
621            err.push(e);
622        }
623        let disp = format!("{}", err);
624        assert_eq!(
625            disp,
626            "Unable to connect to torproject.org: invalid IP address syntax"
627        );
628
629        let disp_alt = format!("{:#}", err);
630        assert!(disp_alt.contains("Unable to connect to torproject.org at 20")); // Year prefix
631        assert!(disp_alt.contains("invalid IP address syntax"));
632    }
633
634    #[test]
635    fn operations() {
636        use std::num::ParseIntError;
637
638        #[derive(From, Clone, Debug, Eq, PartialEq)]
639        struct Wrapper(ParseIntError);
640
641        impl AsRef<dyn Error + 'static> for Wrapper {
642            fn as_ref(&self) -> &(dyn Error + 'static) {
643                &self.0
644            }
645        }
646
647        let mut err: RetryError<Wrapper> = RetryError::in_attempt_to("parse some integers");
648        assert!(err.is_empty());
649        assert_eq!(err.len(), 0);
650        err.extend(
651            vec!["not", "your", "number"]
652                .iter()
653                .filter_map(|s| s.parse::<u16>().err())
654                .map(Wrapper),
655        );
656        assert!(!err.is_empty());
657        assert_eq!(err.len(), 3);
658
659        let cloned = err.clone();
660        for (s1, s2) in err.sources().zip(cloned.sources()) {
661            assert_eq!(s1, s2);
662        }
663
664        err.dedup();
665
666        let disp = format!("{}", err);
667        assert_eq!(
668            disp,
669            "\
670Tried to parse some integers 3 times, but all attempts failed
671Attempts 1..3: invalid digit found in string"
672        );
673
674        let disp_alt = format!("{:#}", err);
675        assert!(disp_alt.contains("Tried to parse some integers 3 times, but all attempts failed"));
676        assert!(disp_alt.contains("(from 20")); // Year prefix for timestamp
677    }
678
679    #[test]
680    fn overflow() {
681        use std::num::ParseIntError;
682        let mut err: RetryError<ParseIntError> =
683            RetryError::in_attempt_to("parse too many integers");
684        assert!(err.is_empty());
685        let mut errors: Vec<ParseIntError> = vec!["no", "numbers"]
686            .iter()
687            .filter_map(|s| s.parse::<u16>().err())
688            .collect();
689        err.n_errors = usize::MAX;
690        err.errors.push((
691            Attempt::Range(1, err.n_errors),
692            errors.pop().expect("parser did not fail"),
693            Instant::now(),
694        ));
695        assert!(err.n_errors == usize::MAX);
696        assert!(err.len() == 1);
697
698        err.push(errors.pop().expect("parser did not fail"));
699        assert!(err.n_errors == usize::MAX);
700        assert!(err.len() == 1);
701    }
702
703    #[test]
704    fn extend_from_retry_preserve_timestamps() {
705        let n1 = Instant::now();
706        let n2 = n1 + Duration::from_secs(10);
707        let n3 = n1 + Duration::from_secs(20);
708
709        let mut err1: RetryError<anyhow::Error> = RetryError::in_attempt_to("do first thing");
710        let mut err2: RetryError<anyhow::Error> = RetryError::in_attempt_to("do second thing");
711
712        err2.push_timed(anyhow::Error::msg("e1"), n1, None);
713        err2.push_timed(anyhow::Error::msg("e2"), n2, None);
714
715        // err1 is empty initially
716        assert!(err1.first_error_at.is_none());
717
718        err1.extend_from_retry_error(err2);
719
720        assert_eq!(err1.len(), 2);
721        // The timestamps should be preserved
722        assert_eq!(err1.errors[0].2, n1);
723        assert_eq!(err1.errors[1].2, n2);
724
725        // Add another error to err1 to ensure mixed sources work
726        err1.push_timed(anyhow::Error::msg("e3"), n3, None);
727        assert_eq!(err1.len(), 3);
728        assert_eq!(err1.errors[2].2, n3);
729    }
730
731    #[test]
732    fn extend_from_retry_preserve_ranges() {
733        let n1 = Instant::now();
734        let mut err1: RetryError<anyhow::Error> = RetryError::in_attempt_to("do thing 1");
735
736        // Push 2 errors
737        err1.push(anyhow::Error::msg("e1"));
738        err1.push(anyhow::Error::msg("e2"));
739        assert_eq!(err1.n_errors, 2);
740
741        let mut err2: RetryError<anyhow::Error> = RetryError::in_attempt_to("do thing 2");
742        // Push 3 identical errors to create a range
743        err2.push_timed(anyhow::Error::msg("repeated"), n1, None);
744        err2.push_timed(anyhow::Error::msg("repeated"), n1, None);
745        err2.push_timed(anyhow::Error::msg("repeated"), n1, None);
746
747        // Dedup err2 so it has a range
748        err2.dedup_by(|e1, e2| e1.to_string() == e2.to_string());
749        assert_eq!(err2.len(), 1); // collapsed to 1 entry
750        match err2.errors[0].0 {
751            Attempt::Range(1, 3) => {}
752            _ => panic!("Expected range 1..3"),
753        }
754
755        // Extend err1 with err2
756        err1.extend_from_retry_error(err2);
757
758        assert_eq!(err1.len(), 3); // 2 singles + 1 range
759        assert_eq!(err1.n_errors, 5); // 2 + 3 = 5 total attempts
760
761        // Check the range indices
762        match err1.errors[2].0 {
763            Attempt::Range(3, 5) => {}
764            ref x => panic!("Expected range 3..5, got {:?}", x),
765        }
766    }
767
768    #[test]
769    fn dedup_after_extend_same_doing() {
770        let doing = "do thing";
771        let message = "error";
772        let n1 = Instant::now();
773        let mut err1: RetryError<anyhow::Error> = RetryError::in_attempt_to(doing);
774
775        // Push 1 error
776        err1.push(anyhow::Error::msg(message));
777        assert_eq!(err1.n_errors, 1);
778
779        let mut err2: RetryError<anyhow::Error> = RetryError::in_attempt_to(doing);
780        // Push 2 identical errors to create a range
781        err2.push_timed(anyhow::Error::msg(message), n1, None);
782        err2.push_timed(anyhow::Error::msg(message), n1, None);
783
784        // Dedup err2 so it has a range
785        err2.dedup_by(|e1, e2| e1.to_string() == e2.to_string());
786        assert_eq!(err2.len(), 1); // collapsed to 1 entry
787        match err2.errors[0].0 {
788            Attempt::Range(1, 2) => {}
789            _ => panic!("Expected range 1..2"),
790        }
791
792        // Extend err1 with err2
793        err1.extend_from_retry_error(err2);
794        assert_eq!(err1.len(), 2); // 1 single + 1 range
795        assert_eq!(err1.n_errors, 3); // 1 + 2 = 3 total attempts
796
797        // Dedup err1 so it has only one range
798        err1.dedup_by(|e1, e2| e1.to_string() == e2.to_string());
799        assert_eq!(err1.len(), 1); // collapsed to 1 entry
800        assert_eq!(err1.n_errors, 3); // 3 total attempts
801
802        // Check the range indices
803        match err1.errors[0].0 {
804            Attempt::Range(1, 3) => {}
805            ref x => panic!("Expected range 1..3, got {:?}", x),
806        }
807    }
808}