1use alloc::vec::Vec;
2use core::fmt;
3
4use retry_policy::StopReason as RetryPolicyStopReason;
5
6pub struct Error<T> {
8 pub stop_reason: RetryPolicyStopReason,
9 errors: Vec<T>,
10}
11
12impl<T> Error<T> {
13 pub(crate) fn new(stop_reason: RetryPolicyStopReason, errors: Vec<T>) -> Self {
14 assert!(!errors.is_empty());
15
16 Self {
17 stop_reason,
18 errors,
19 }
20 }
21
22 pub fn last_error(mut self) -> T {
23 self.errors.pop().expect("unreachable!()")
24 }
25
26 pub fn errors(self) -> Vec<T> {
27 self.errors
28 }
29}
30
31impl<T> fmt::Debug for Error<T>
32where
33 T: fmt::Debug,
34{
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("Error")
37 .field("stop_reason", &self.stop_reason)
38 .field("errors", &self.errors)
39 .finish()
40 }
41}
42
43impl<T> fmt::Display for Error<T>
44where
45 T: fmt::Debug,
46{
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(f, "{self:?}")
49 }
50}
51
52#[cfg(feature = "std")]
53impl<T> std::error::Error for Error<T> where T: fmt::Debug {}