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

#[derive(Debug, Serialize, Deserialize)]
struct Error {
    code: i32,
    message: String,
}

impl Default for Error {
    fn default() -> Self {
        Self {
            code: reqwest::StatusCode::INTERNAL_SERVER_ERROR.as_u16() as i32,
            message: reqwest::StatusCode::INTERNAL_SERVER_ERROR
                .as_str()
                .to_owned(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ClientError {
    id: u16,
    jsonrpc: String,
    error: Error,
}

impl std::error::Error for ClientError {}

impl Default for ClientError {
    fn default() -> Self {
        Self {
            id: 0,
            jsonrpc: String::from("2.0"),
            error: Error::default(),
        }
    }
}

impl From<reqwest::Error> for ClientError {
    fn from(error: reqwest::Error) -> Self {
        ClientError {
            error: Error {
                code: error
                    .status()
                    .unwrap_or(
                        reqwest::StatusCode::from_u16(ClientError::default().error.code as u16)
                            .unwrap(),
                    )
                    .as_u16() as i32,
                message: error.to_string(),
            },
            ..Default::default()
        }
    }
}

impl ClientError {
    pub fn new(error_msg: &str) -> Self {
        ClientError {
            error: Error {
                code: reqwest::StatusCode::SEE_OTHER.as_u16() as i32,
                message: error_msg.to_string(),
            },
            ..Default::default()
        }
    }
}

impl fmt::Display for ClientError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(format!("Client error: {}", self.error.message).as_str())
    }
}