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
use serde::{Deserialize, Serialize};
use std::fmt;
use url::ParseError;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ErrorInformation {
pub error: String,
#[serde(default)]
pub message: String,
}
#[derive(thiserror::Error, Debug)]
pub enum ClientError<E>
where
E: std::error::Error + Send + Sync + 'static,
{
#[error("client error: {0}")]
Client(#[from] Box<E>),
#[error("request error: {0}")]
Request(String),
#[error("service error: {0}")]
Service(ErrorInformation),
#[error("token error: {0}")]
Token(#[source] Box<dyn std::error::Error + Send + Sync>),
#[error("Url parse error")]
Url(#[from] ParseError),
#[error("Syntax error: {0}")]
Syntax(#[source] Box<dyn std::error::Error + Send + Sync>),
}
impl<E> ClientError<E>
where
E: std::error::Error + Send + Sync + 'static,
{
pub fn syntax<S>(err: S) -> ClientError<E>
where
S: std::error::Error + Send + Sync + 'static,
{
Self::Syntax(Box::new(err))
}
}
#[cfg(feature = "reqwest")]
impl From<reqwest::Error> for ClientError<reqwest::Error> {
fn from(err: reqwest::Error) -> Self {
ClientError::Client(Box::new(err))
}
}
impl<E> From<serde_json::Error> for ClientError<E>
where
E: std::error::Error + Send + Sync + 'static,
{
fn from(err: serde_json::Error) -> Self {
ClientError::Syntax(Box::new(err))
}
}
impl fmt::Display for ErrorInformation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.error, self.message)
}
}