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
use serde::Deserialize;
use serde_json;
use std::error::Error as StdError;
use std::fmt;
#[derive(Debug)]
pub struct Error {
    pub kind: ErrorKind,
}

impl StdError for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            ErrorKind::HTTP(_) => {
                write!(f, "http error")
            }
            ErrorKind::Status(err) => {
                write!(f, "status code: {}, message: {}", err.code, err.message)
            }
            ErrorKind::JSON(_) => {
                write!(f, "json error")
            }
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Self {
            kind: ErrorKind::HTTP(e),
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Self {
            kind: ErrorKind::JSON(e),
        }
    }
}

impl Error {
    pub fn new(kind: ErrorKind) -> Self {
        Self { kind }
    }
}

#[derive(Debug)]
pub enum ErrorKind {
    HTTP(reqwest::Error),
    Status(StatusError),
    JSON(serde_json::Error),
}

#[derive(Debug)]
pub struct StatusError {
    pub code: u16,
    pub message: String,
}

impl StatusError {
    pub fn new(code: u16, message: String) -> Self {
        Self { code, message }
    }
}
#[derive(Deserialize)]
pub struct ErrorMessage {
    pub message: String
}