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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::api_error::ApiError;
use hyper::{Body, Response, StatusCode};
use std::{
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(not(feature = "simd-json"))]
use serde_json::Error as JsonError;
#[cfg(feature = "simd-json")]
use simd_json::Error as JsonError;
#[derive(Debug)]
pub struct Error {
pub(super) source: Option<Box<dyn StdError + Send + Sync>>,
pub(super) kind: ErrorType,
}
impl Error {
#[must_use = "retrieving the type has no effect if left unused"]
pub const fn kind(&self) -> &ErrorType {
&self.kind
}
#[must_use = "consuming the error and retrieving the source has no effect if left unused"]
pub fn into_source(self) -> Option<Box<dyn StdError + Send + Sync>> {
self.source
}
#[must_use = "consuming the error into its parts has no effect if left unused"]
pub fn into_parts(self) -> (ErrorType, Option<Box<dyn StdError + Send + Sync>>) {
(self.kind, self.source)
}
pub(super) fn json(source: JsonError) -> Self {
Self {
kind: ErrorType::Json,
source: Some(Box::new(source)),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.kind {
ErrorType::BuildingRequest => f.write_str("failed to build the request"),
ErrorType::ChunkingResponse => f.write_str("Chunking the response failed"),
ErrorType::CreatingHeader { name, .. } => {
write!(f, "Parsing the value for header {} failed", name)
}
ErrorType::Json => f.write_str("Given value couldn't be serialized"),
ErrorType::Parsing { body, .. } => {
write!(f, "Response body couldn't be deserialized: {:?}", body)
}
ErrorType::RequestCanceled => {
f.write_str("Request was canceled either before or while being sent")
}
ErrorType::RequestError => f.write_str("Parsing or sending the response failed"),
ErrorType::RequestTimedOut => f.write_str("request timed out"),
ErrorType::Response { error, status, .. } => write!(
f,
"Response error: status code {}, error: {}",
status, error
),
ErrorType::ServiceUnavailable { .. } => {
f.write_str("api may be temporarily unavailable (received a 503)")
}
ErrorType::Unauthorized => {
f.write_str("token in use is invalid, expired, or is revoked")
}
}
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.source
.as_ref()
.map(|source| &**source as &(dyn StdError + 'static))
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorType {
BuildingRequest,
ChunkingResponse,
CreatingHeader {
name: String,
},
Json,
Parsing {
body: Vec<u8>,
},
RequestCanceled,
RequestError,
RequestTimedOut,
Response {
body: Vec<u8>,
error: ApiError,
status: StatusCode,
},
ServiceUnavailable {
response: Response<Body>,
},
Unauthorized,
}