zerokms_protocol/
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
use static_assertions::assert_impl_all;
use std::error::Error as StdError;
use thiserror::Error;

pub trait RetryableError {
    fn can_retry(&self) -> bool;
}

type ShareableError = Box<dyn StdError + Send + Sync>;

#[derive(Debug)]
pub enum ViturRequestErrorKind {
    PrepareRequest,
    SendRequest,
    NotFound,
    FailureResponse,
    ParseResponse,
    Other,
}

impl std::fmt::Display for ViturRequestErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::PrepareRequest => "PrepareRequest",
                Self::SendRequest => "SendRequest",
                Self::NotFound => "NotFound",
                Self::FailureResponse => "FailureResponse",
                Self::ParseResponse => "ParseResponse",
                Self::Other => "Other",
            }
        )
    }
}

#[derive(Debug, Error)]
#[error("{kind}: {message}: {error}")]
pub struct ViturRequestError {
    pub kind: ViturRequestErrorKind,
    pub message: &'static str,
    pub error: ShareableError,
    can_retry: bool,
}

// ViturRequestError should be able to be sent and shared between threads to be able to support
// async as well as `anyhow!` for async.
assert_impl_all!(ViturRequestError: Send, Sync);

impl ViturRequestError {
    #[inline]
    pub fn prepare(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::PrepareRequest,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    #[inline]
    pub fn parse(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::ParseResponse,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    #[inline]
    pub fn send(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::SendRequest,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    #[inline]
    pub fn not_found(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::NotFound,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    #[inline]
    pub fn response(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::FailureResponse,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    #[inline]
    pub fn other(message: &'static str, error: impl StdError + 'static + Send + Sync) -> Self {
        Self {
            kind: ViturRequestErrorKind::Other,
            message,
            error: Box::new(error),
            can_retry: false,
        }
    }

    pub fn with_retryable(mut self, can_retry: bool) -> Self {
        self.can_retry = can_retry;
        self
    }

    pub fn retryable(self) -> Self {
        self.with_retryable(true)
    }
}

impl RetryableError for ViturRequestError {
    fn can_retry(&self) -> bool {
        self.can_retry
    }
}