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
use reqwest::StatusCode;
#[derive(Debug, serde::Deserialize)]
pub struct ServerBodyError {
pub status_code: u16,
pub status_message: String,
}
#[derive(Debug)]
pub struct ServerError {
pub code: StatusCode,
pub body: ServerBodyError,
}
#[derive(Debug)]
pub enum Error {
Reqwest(reqwest::Error),
Server(ServerError),
}
impl Error {
pub fn as_reqwest_error(&self) -> Option<&reqwest::Error> {
match self {
Self::Reqwest(inner) => Some(inner),
_ => None,
}
}
pub fn is_reqwest_error(&self) -> bool {
matches!(self, Self::Reqwest(_))
}
pub fn as_server_error(&self) -> Option<&ServerError> {
match self {
Self::Server(inner) => Some(inner),
_ => None,
}
}
pub fn is_server_error(&self) -> bool {
matches!(self, Self::Server(_))
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Self::Reqwest(err)
}
}
impl From<(StatusCode, ServerBodyError)> for Error {
fn from((code, body): (StatusCode, ServerBodyError)) -> Self {
Self::Server(ServerError { code, body })
}
}