lmstfy_client/
errors.rs

1use std::cmp::PartialEq;
2use std::error;
3use std::fmt;
4
5/// Create an `enum` to classify api error.
6#[derive(Debug, Clone, PartialEq)]
7pub enum ErrType {
8    RequestErr,
9    ResponseErr,
10    Other,
11}
12
13impl Default for ErrType {
14    fn default() -> Self {
15        ErrType::Other
16    }
17}
18
19impl fmt::Display for ErrType {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        let printable = match *self {
22            ErrType::RequestErr => "req",
23            ErrType::ResponseErr => "resp",
24            ErrType::Other => "",
25        };
26
27        write!(f, "{}", printable)
28    }
29}
30
31#[derive(Debug, Clone, Default)]
32pub struct APIError {
33    pub err_type: ErrType,
34    pub reason: String,
35    pub job_id: String,
36    pub request_id: String,
37}
38
39impl error::Error for APIError {
40    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
41        None
42    }
43}
44
45impl fmt::Display for APIError {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        write!(
48            f,
49            "t:{}; m:{}; j:{}; r:{}",
50            self.err_type, self.reason, self.job_id, self.request_id
51        )
52    }
53}