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
use std::fmt;

use serde_json;

use thiserror::Error;

/// Common set of error module exports
pub mod prelude {
    pub use super::{err_msg, input_err, VdrError, VdrErrorKind, VdrResult, VdrResultExt};
}

/// Library error type
#[derive(Debug, Error)]
pub struct VdrError {
    kind: VdrErrorKind,
    msg: Option<String>,
    #[source]
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    // backtrace (when supported)
}

/// Supported error kinds for `VdrError`
#[derive(Debug, Error)]
pub enum VdrErrorKind {
    // General errors
    #[error("Configuration error")]
    Config,
    #[error("Connection error")]
    Connection,
    #[error("File system error")]
    FileSystem,
    #[error("Input error")]
    Input,
    #[error("Resource error")]
    Resource,
    #[error("Service unavailable")]
    Unavailable,
    #[error("Unexpected error")]
    Unexpected,
    #[error("Incompatible error")]
    Incompatible,
    // Transaction errors
    #[error("No consensus from verifiers")]
    PoolNoConsensus,
    #[error("Request failed: {}", pool_request_failed_reason(.0))]
    PoolRequestFailed(String),
    #[error("Pool timeout")]
    PoolTimeout,
    #[error("Resolver error")]
    Resolver,
}

impl VdrError {
    pub fn new(
        kind: VdrErrorKind,
        msg: Option<String>,
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    ) -> Self {
        Self { kind, msg, source }
    }

    pub fn kind(&self) -> &VdrErrorKind {
        &self.kind
    }

    pub fn extra(&self) -> Option<String> {
        match self.kind {
            VdrErrorKind::PoolRequestFailed(ref response) => Some(response.clone()),
            _ => None,
        }
    }

    pub fn with_source<E>(mut self, source: E) -> Self
    where
        E: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        self.source.replace(source.into());
        self
    }
}

impl fmt::Display for VdrError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (&self.kind, &self.msg) {
            (VdrErrorKind::Input, None) => write!(f, "{}", self.kind),
            (VdrErrorKind::Input, Some(msg)) => f.write_str(msg),
            (kind, None) => write!(f, "{}", kind),
            (kind, Some(msg)) => write!(f, "{}: {}", kind, msg),
        }?;
        if let Some(ref source) = self.source {
            write!(f, "\n{}", source)?;
        }
        Ok(())
    }
}

impl From<VdrError> for VdrErrorKind {
    fn from(error: VdrError) -> VdrErrorKind {
        error.kind
    }
}

impl From<VdrErrorKind> for VdrError {
    fn from(kind: VdrErrorKind) -> VdrError {
        VdrError::new(kind, None, None)
    }
}

impl From<crate::utils::ConversionError> for VdrError {
    fn from(err: crate::utils::ConversionError) -> Self {
        VdrError::new(VdrErrorKind::Input, Some(err.to_string()), None)
    }
}

impl From<crate::utils::ValidationError> for VdrError {
    fn from(err: crate::utils::ValidationError) -> Self {
        VdrError::new(VdrErrorKind::Input, Some(err.to_string()), None)
    }
}

impl From<std::io::Error> for VdrError {
    fn from(err: std::io::Error) -> VdrError {
        VdrError::new(VdrErrorKind::FileSystem, None, Some(Box::new(err)))
    }
}

impl From<zmq::Error> for VdrError {
    fn from(err: zmq::Error) -> VdrError {
        VdrError::new(VdrErrorKind::Connection, None, Some(Box::new(err)))
    }
}

impl From<sled::Error> for VdrError {
    fn from(err: sled::Error) -> VdrError {
        VdrError::new(VdrErrorKind::FileSystem, None, Some(Box::new(err)))
    }
}

impl<M> From<(VdrErrorKind, M)> for VdrError
where
    M: fmt::Display + Send + Sync + 'static,
{
    fn from((kind, msg): (VdrErrorKind, M)) -> VdrError {
        VdrError::new(kind, Some(msg.to_string()), None)
    }
}

/// Construct a `VdrError` from an error kind and message
pub fn err_msg<M>(kind: VdrErrorKind, msg: M) -> VdrError
where
    M: fmt::Display + Send + Sync + 'static,
{
    (kind, msg.to_string()).into()
}

/// Construct a `VdrError` input error from an error message
pub fn input_err<M>(msg: M) -> VdrError
where
    M: fmt::Display + Send + Sync + 'static,
{
    (VdrErrorKind::Input, msg.to_string()).into()
}

/// Library result type
pub type VdrResult<T> = Result<T, VdrError>;

/// Trait providing utility methods for handling other result types
pub trait VdrResultExt<T, E> {
    fn map_err_string(self) -> Result<T, String>;
    fn map_input_err<F, M>(self, mapfn: F) -> VdrResult<T>
    where
        F: FnOnce() -> M,
        M: fmt::Display + Send + Sync + 'static;
    fn with_err_msg<M>(self, kind: VdrErrorKind, msg: M) -> VdrResult<T>
    where
        M: fmt::Display + Send + Sync + 'static;
    fn with_input_err<M>(self, msg: M) -> VdrResult<T>
    where
        M: fmt::Display + Send + Sync + 'static;
}

impl<T, E> VdrResultExt<T, E> for Result<T, E>
where
    E: std::error::Error + Send + Sync + 'static,
{
    fn map_err_string(self) -> Result<T, String> {
        self.map_err(|err| err.to_string())
    }

    fn map_input_err<F, M>(self, mapfn: F) -> VdrResult<T>
    where
        F: FnOnce() -> M,
        M: fmt::Display + Send + Sync + 'static,
    {
        self.map_err(|err| {
            VdrError::new(
                VdrErrorKind::Input,
                Some(mapfn().to_string()),
                Some(Box::new(err)),
            )
        })
    }

    fn with_err_msg<M>(self, kind: VdrErrorKind, msg: M) -> VdrResult<T>
    where
        M: fmt::Display + Send + Sync + 'static,
    {
        self.map_err(|err| VdrError::new(kind, Some(msg.to_string()), Some(Box::new(err))))
    }

    fn with_input_err<M>(self, msg: M) -> VdrResult<T>
    where
        M: fmt::Display + Send + Sync + 'static,
    {
        self.map_err(|err| {
            VdrError::new(
                VdrErrorKind::Input,
                Some(msg.to_string()),
                Some(Box::new(err)),
            )
        })
    }
}

fn pool_request_failed_reason(reply: &str) -> String {
    if let Ok(reply) = serde_json::from_str::<serde_json::Value>(reply) {
        if let Some(reason) = reply["reason"].as_str() {
            return reason.to_owned();
        }
    }
    "Unknown reason".to_owned()
}