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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::retry::{ErrorKind, ProvideErrorKind};
use std::collections::HashMap;
use std::fmt;
pub mod display;
#[derive(Debug, Eq, PartialEq, Default, Clone)]
pub struct Error {
code: Option<String>,
message: Option<String>,
request_id: Option<String>,
extras: HashMap<&'static str, String>,
}
#[derive(Debug, Default)]
pub struct Builder {
inner: Error,
}
impl Builder {
pub fn message(&mut self, message: impl Into<String>) -> &mut Self {
self.inner.message = Some(message.into());
self
}
pub fn code(&mut self, code: impl Into<String>) -> &mut Self {
self.inner.code = Some(code.into());
self
}
pub fn request_id(&mut self, request_id: impl Into<String>) -> &mut Self {
self.inner.request_id = Some(request_id.into());
self
}
pub fn custom(&mut self, key: &'static str, value: impl Into<String>) -> &mut Self {
self.inner.extras.insert(key, value.into());
self
}
pub fn build(&mut self) -> Error {
std::mem::take(&mut self.inner)
}
}
impl Error {
pub fn code(&self) -> Option<&str> {
self.code.as_deref()
}
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
pub fn request_id(&self) -> Option<&str> {
self.request_id.as_deref()
}
pub fn extra(&self, key: &'static str) -> Option<&str> {
self.extras.get(key).map(|k| k.as_str())
}
pub fn builder() -> Builder {
Builder::default()
}
pub fn into_builder(self) -> Builder {
Builder { inner: self }
}
}
impl ProvideErrorKind for Error {
fn retryable_error_kind(&self) -> Option<ErrorKind> {
None
}
fn code(&self) -> Option<&str> {
Error::code(self)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut fmt = f.debug_struct("Error");
if let Some(code) = &self.code {
fmt.field("code", code);
}
if let Some(message) = &self.message {
fmt.field("message", message);
}
if let Some(req_id) = &self.request_id {
fmt.field("request_id", req_id);
}
for (k, v) in &self.extras {
fmt.field(k, &v);
}
fmt.finish()
}
}
impl std::error::Error for Error {}
#[derive(Debug)]
pub(super) enum TryFromNumberErrorKind {
OutsideIntegerRange(std::num::TryFromIntError),
U64ToFloatLossyConversion(u64),
I64ToFloatLossyConversion(i64),
F64ToF32LossyConversion(f64),
FloatToIntegerLossyConversion(f64),
NegativeToUnsignedLossyConversion(i64),
}
#[derive(Debug)]
pub struct TryFromNumberError {
pub(super) kind: TryFromNumberErrorKind,
}
impl fmt::Display for TryFromNumberError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use TryFromNumberErrorKind::*;
match self.kind {
OutsideIntegerRange(_) => write!(f, "integer too large"),
FloatToIntegerLossyConversion(v) => write!(
f,
"cannot convert floating point number {v} into an integer"
),
NegativeToUnsignedLossyConversion(v) => write!(
f,
"cannot convert negative integer {v} into an unsigned integer type"
),
U64ToFloatLossyConversion(v) => {
write!(
f,
"cannot convert {v}u64 into a floating point type without precision loss"
)
}
I64ToFloatLossyConversion(v) => {
write!(
f,
"cannot convert {v}i64 into a floating point type without precision loss"
)
}
F64ToF32LossyConversion(v) => {
write!(f, "will not attempt to convert {v}f64 into a f32")
}
}
}
}
impl std::error::Error for TryFromNumberError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use TryFromNumberErrorKind::*;
match &self.kind {
OutsideIntegerRange(err) => Some(err as _),
FloatToIntegerLossyConversion(_)
| NegativeToUnsignedLossyConversion(_)
| U64ToFloatLossyConversion(_)
| I64ToFloatLossyConversion(_)
| F64ToF32LossyConversion(_) => None,
}
}
}
impl From<std::num::TryFromIntError> for TryFromNumberError {
fn from(value: std::num::TryFromIntError) -> Self {
Self {
kind: TryFromNumberErrorKind::OutsideIntegerRange(value),
}
}
}
impl From<TryFromNumberErrorKind> for TryFromNumberError {
fn from(kind: TryFromNumberErrorKind) -> Self {
Self { kind }
}
}