go_zoom_kinesis/retry/
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
use crate::{retry, ProcessorError};
use std::time::Duration;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum RetryError {
    #[error("Operation timed out after {0:?}")]
    Timeout(Duration),

    #[error("Maximum retries ({0}) exceeded: {1}")]
    MaxRetriesExceeded(u32, String),

    #[error("Retry interrupted by shutdown signal")]
    Interrupted,

    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl From<retry::RetryError> for ProcessorError {
    fn from(err: retry::RetryError) -> Self {
        match err {
            retry::RetryError::Timeout(d) => ProcessorError::ProcessingTimeout(d),
            retry::RetryError::MaxRetriesExceeded(attempts, msg) => {
                ProcessorError::MaxRetriesExceeded(format!("After {} attempts: {}", attempts, msg))
            }
            retry::RetryError::Interrupted => ProcessorError::Shutdown,
            retry::RetryError::Other(e) => ProcessorError::Other(e),
        }
    }
}

impl RetryError {
    pub fn is_timeout(&self) -> bool {
        matches!(self, RetryError::Timeout(_))
    }

    pub fn is_max_retries(&self) -> bool {
        matches!(self, RetryError::MaxRetriesExceeded(_, _))
    }

    pub fn is_interrupted(&self) -> bool {
        matches!(self, RetryError::Interrupted)
    }
}