tokio_retry2/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use std::error;
use std::fmt;

use std::time::Duration;

const TRANSIENT_ERROR: &str = "transient error";
const PERMANENT_ERROR: &str = "permanent error";

/// `Error` is the error value in an actions's retry result.
///
/// Based on the two possible values, the operation
/// may be retried.
pub enum Error<E> {
    /// `Permanent` means that it's impossible to execute the operation
    /// successfully. This error is an early return from the retry operation.
    Permanent(E),

    /// `Transient` means that the error is temporary. If the `retry_after` is `None`
    /// the operation should be retried according to the defined strategy policy, else after
    /// the specified duration. Useful for handling ratelimits like a HTTP 429 response.
    Transient {
        err: E,
        retry_after: Option<Duration>,
    },
}

impl<E> Error<E> {
    // Creates an permanent error.
    pub fn permanent(err: E) -> Self {
        Error::Permanent(err)
    }

    // Creates a Result::Err container with an permanent error.
    pub fn to_permanent<T>(err: E) -> Result<T, Self> {
        Err(Error::Permanent(err))
    }

    // Creates an transient error which is retried according to the defined strategy
    // policy.
    pub fn transient(err: E) -> Self {
        Error::Transient {
            err,
            retry_after: None,
        }
    }

    // Creates a Result::Err container with an transient error which
    // is retried according to the defined strategy policy.
    pub fn to_transient<T>(err: E) -> Result<T, Self> {
        Err(Error::Transient {
            err,
            retry_after: None,
        })
    }

    /// Creates a Result::Err container with a transient error which
    /// is retried after the specified duration.
    /// Useful for handling ratelimits like a HTTP 429 response.
    pub fn to_retry_after<T>(err: E, duration: Duration) -> Result<T, Self> {
        Err(Error::Transient {
            err,
            retry_after: Some(duration),
        })
    }

    /// Creates a transient error which is retried after the specified duration.
    /// Useful for handling ratelimits like a HTTP 429 response.
    pub fn retry_after(err: E, duration: Duration) -> Self {
        Error::Transient {
            err,
            retry_after: Some(duration),
        }
    }
}

impl<E> fmt::Display for Error<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            Error::Permanent(ref err)
            | Error::Transient {
                ref err,
                retry_after: _,
            } => err.fmt(f),
        }
    }
}

impl<E> fmt::Debug for Error<E>
where
    E: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        let (name, err) = match *self {
            Error::Permanent(ref err) => ("Permanent", err as &dyn fmt::Debug),
            Error::Transient {
                ref err,
                retry_after: _,
            } => ("Transient", err as &dyn fmt::Debug),
        };
        f.debug_tuple(name).field(err).finish()
    }
}

impl<E> error::Error for Error<E>
where
    E: error::Error,
{
    fn description(&self) -> &str {
        match *self {
            Error::Permanent(_) => PERMANENT_ERROR,
            Error::Transient { .. } => TRANSIENT_ERROR,
        }
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            Error::Permanent(ref err)
            | Error::Transient {
                ref err,
                retry_after: _,
            } => err.source(),
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        self.source()
    }
}

/// By default all errors are transient. Permanent errors can
/// be constructed explicitly. This implementation is for making
/// the question mark operator (?) and the `try!` macro to work.
impl<E> From<E> for Error<E> {
    fn from(err: E) -> Error<E> {
        Error::Transient {
            err,
            retry_after: None,
        }
    }
}

impl<E> PartialEq for Error<E>
where
    E: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Error::Permanent(ref self_err), Error::Permanent(ref other_err)) => {
                self_err == other_err
            }
            (
                Error::Transient {
                    err: self_err,
                    retry_after: self_retry_after,
                },
                Error::Transient {
                    err: other_err,
                    retry_after: other_retry_after,
                },
            ) => self_err == other_err && self_retry_after == other_retry_after,
            _ => false,
        }
    }
}

#[cfg(feature = "implicit_results")]
#[derive(Debug, PartialEq)]
pub enum RetryResult<T, E> {
    Ok(T),
    Err(Error<E>),
}

#[cfg(feature = "implicit_results")]
impl<T, E> From<Result<T, Error<E>>> for RetryResult<T, E> {
    fn from(r: Result<T, Error<E>>) -> RetryResult<T, E> {
        match r {
            Ok(t) => Self::Ok(t),
            Err(e) => Self::Err(e),
        }
    }
}

#[cfg(feature = "implicit_results")]
impl<T, E> From<Result<T, E>> for RetryResult<T, E> {
    fn from(r: Result<T, E>) -> RetryResult<T, E> {
        r.map_transient_err().into()
    }
}

#[cfg(feature = "implicit_results")]
impl<T, E> From<RetryResult<T, E>> for Result<T, Error<E>> {
    fn from(value: RetryResult<T, E>) -> Self {
        match value {
            RetryResult::Ok(t) => Ok(t),
            RetryResult::Err(error) => Err(error),
        }
    }
}

#[cfg(feature = "implicit_results")]
impl<T: PartialEq, E: PartialEq> PartialEq<RetryResult<T, E>> for Result<T, Error<E>> {
    fn eq(&self, other: &RetryResult<T, E>) -> bool {
        match (self, other) {
            (Ok(self_t), RetryResult::Ok(other_t)) => self_t == other_t,
            (Err(self_err), RetryResult::Err(other_err)) => self_err == other_err,
            _ => false,
        }
    }
}

#[cfg(feature = "implicit_results")]
impl<T> From<Option<T>> for RetryResult<T, String> {
    fn from(r: Option<T>) -> RetryResult<T, String> {
        match r {
            Some(t) => Self::Ok(t),
            None => Self::Err(Error::Transient {
                err: String::from(TRANSIENT_ERROR),
                retry_after: None,
            }),
        }
    }
}

pub trait MapErr<T, E> {
    fn map_transient_err(self) -> Result<T, Error<E>>;
    fn map_permanent_err(self) -> Result<T, Error<E>>;
}

impl<T, E> MapErr<T, E> for Result<T, E> {
    #[inline]
    fn map_transient_err(self) -> Result<T, Error<E>> {
        match self {
            Ok(t) => Ok(t),
            Err(e) => Err(Error::transient(e)),
        }
    }

    #[inline]
    fn map_permanent_err(self) -> Result<T, Error<E>> {
        match self {
            Ok(t) => Ok(t),
            Err(e) => Err(Error::permanent(e)),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::error::Error as StdError;
    use std::fmt;

    #[test]
    fn create_permanent_error() {
        let e = Error::permanent("err");
        assert_eq!(e, Error::Permanent("err"));
    }

    #[test]
    fn create_transient_error() {
        let e = Error::transient("err");
        assert_eq!(
            e,
            Error::Transient {
                err: "err",
                retry_after: None
            }
        );
    }

    #[test]
    fn create_transient_error_with_retry_after() {
        let retry_after = Duration::from_secs(42);
        let e = Error::retry_after("err", retry_after);
        assert_eq!(
            e,
            Error::Transient {
                err: "err",
                retry_after: Some(retry_after),
            }
        );
    }

    #[test]
    fn map_transient_keeps_ok() {
        let result: Result<i32, Error<()>> = Ok(42).map_transient_err();
        assert_eq!(result, Ok(42));
    }

    #[test]
    fn map_transient_maps_err() {
        let result: Result<(), Error<&str>> = Err("err").map_transient_err();
        assert_eq!(
            result,
            Err::<(), Error<&str>>(Error::Transient {
                err: "err",
                retry_after: None
            })
        );
    }

    #[test]
    fn map_permanent_keeps_ok() {
        let result: Result<i32, Error<()>> = Ok(42).map_permanent_err();
        assert_eq!(result, Ok(42));
    }

    #[test]
    fn map_permanent_maps_err() {
        let result: Result<(), Error<&str>> = Err("err").map_permanent_err();
        assert_eq!(result, Err(Error::Permanent("err")));
    }

    #[test]
    fn fmt_permanent_error() {
        let error = Error::Permanent(PERMANENT_ERROR);
        let formatted = format!("{}", error);
        assert_eq!(formatted, PERMANENT_ERROR);
    }

    #[test]
    fn fmt_transient_error() {
        let error = Error::Transient {
            err: TRANSIENT_ERROR,
            retry_after: None,
        };
        let formatted = format!("{}", error);
        assert_eq!(formatted, TRANSIENT_ERROR);
    }

    #[test]
    fn debug_permanent_error() {
        let error = Error::Permanent(PERMANENT_ERROR);
        let debug = format!("{:?}", error);
        assert_eq!(debug, "Permanent(\"permanent error\")");
    }

    #[test]
    fn debug_transient_error() {
        let error = Error::Transient {
            err: TRANSIENT_ERROR,
            retry_after: None,
        };
        let debug = format!("{:?}", error);
        assert_eq!(debug, "Transient(\"transient error\")");
    }

    #[test]
    fn description_permanent_error() {
        let error = Error::permanent(MyError(PERMANENT_ERROR));
        assert_eq!(error.description(), PERMANENT_ERROR);
    }

    #[test]
    fn description_transient_error() {
        let error = Error::transient(MyError(TRANSIENT_ERROR));
        assert_eq!(error.description(), TRANSIENT_ERROR);
    }

    #[test]
    fn source_permanent_error() {
        let error: Result<(), Error<MyError>> =
            Error::to_retry_after(MyError(TRANSIENT_ERROR), std::time::Duration::from_secs(1));
        assert!(error.unwrap_err().source().is_none());
    }

    #[test]
    fn source_transient_error() {
        let error = Error::retry_after(MyError(TRANSIENT_ERROR), std::time::Duration::from_secs(1));
        assert!(error.source().is_none());
    }

    #[test]
    fn cause_permanent_error() {
        let error = Error::permanent(MyError(PERMANENT_ERROR));
        assert!(error.cause().is_none());
    }

    #[test]
    fn cause_transient_error() {
        let error = Error::transient(MyError(TRANSIENT_ERROR));
        assert!(error.cause().is_none());
    }

    #[derive(Debug, PartialEq)]
    pub struct MyError(pub &'static str);
    impl fmt::Display for MyError {
        fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
            write!(f, "{}", self.0)
        }
    }
    impl StdError for MyError {
        fn description(&self) -> &str {
            self.0
        }

        fn source(&self) -> Option<&(dyn StdError + 'static)> {
            None
        }

        fn cause(&self) -> Option<&dyn StdError> {
            self.source()
        }
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_ok() {
        let result: Result<i32, ()> = Ok(42);
        let retry_result: RetryResult<i32, ()> = result.into();
        assert_eq!(retry_result, Ok::<i32, ()>(42).into());
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_err_permanent() {
        let error = Error::Permanent(PERMANENT_ERROR);
        let result: Result<i32, Error<&str>> = Err(error);
        let retry_result: RetryResult<i32, &str> = result.into();
        assert_eq!(retry_result, Err(Error::Permanent(PERMANENT_ERROR)).into());
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_err_transient() {
        let error = Error::Transient {
            err: TRANSIENT_ERROR,
            retry_after: None,
        };
        let result: Result<i32, Error<&str>> = Err(error);
        let retry_result: RetryResult<i32, &str> = result.into();
        assert_eq!(
            retry_result,
            Err(Error::Transient {
                err: TRANSIENT_ERROR,
                retry_after: None
            })
            .into()
        );
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_result_with_error() {
        let error = MyError("my error");
        let result = Err(error);
        let retry_result: RetryResult<i32, MyError> = result.into();
        assert_eq!(
            retry_result,
            Err(Error::Transient {
                err: MyError("my error"),
                retry_after: None
            })
            .into()
        );
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_result_with_transient_error() {
        let error = MyError("my transient error");
        let result = Err(error);
        let retry_result: RetryResult<i32, MyError> = result.into();
        assert_eq!(
            retry_result,
            Err(Error::Transient {
                err: MyError("my transient error"),
                retry_after: None
            })
            .into()
        );
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_some() {
        let option = Some(42);
        let retry_result: RetryResult<i32, String> = option.into();
        assert_eq!(retry_result, Ok::<i32, String>(42).into());
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn from_none() {
        let option: Option<i32> = None;
        let retry_result: RetryResult<i32, String> = option.into();
        assert_eq!(
            retry_result,
            Err(Error::Transient {
                err: String::from(TRANSIENT_ERROR),
                retry_after: None
            })
            .into()
        );
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn partial_eq_on_ok() {
        let option = Some(42);
        let retry_result: RetryResult<i32, String> = option.into();
        let result: Result<i32, Error<String>> = Ok(42);
        assert_eq!(result, retry_result);
    }

    #[test]
    #[cfg(feature = "implicit_results")]
    fn partial_eq_on_err() {
        let option = None::<i32>;
        let retry_result: RetryResult<i32, String> = option.into();
        let result: Result<i32, Error<String>> = Err(Error::transient(TRANSIENT_ERROR.to_string()));
        assert_eq!(result, retry_result);
    }
}