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
use std::{fmt, error, io};
use error::Error as ErrorTrait;
use http::header::StatusCode;
use http::bytes_stream::SizeLimitReached;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
source: Option<Box<dyn ErrorTrait + Send + Sync>>
}
impl Error {
pub fn new<K, E>(kind: K, error: E) -> Self
where
K: Into<ErrorKind>,
E: Into<Box<dyn ErrorTrait + Send + Sync>> {
Self {
kind: kind.into(),
source: Some(error.into())
}
}
pub fn empty<K>(kind: K) -> Self
where K: Into<ErrorKind> {
Self {
kind: kind.into(),
source: None
}
}
pub fn status_code(&self) -> StatusCode {
match self.kind {
ErrorKind::Client(c) => c.into(),
ErrorKind::Server(s) => s.into()
}
}
pub fn from_client_io(error: io::Error) -> Self {
Self::new(
ClientErrorKind::from_io(&error),
error
)
}
pub fn from_server_error<E>(error: E) -> Self
where E: Into<Box<dyn ErrorTrait + Send + Sync>> {
Self::new(ServerErrorKind::InternalServerError, error)
}
}
impl<T> From<T> for Error
where T: Into<ErrorKind> {
fn from(e: T) -> Self {
Self::empty(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
self.source.as_ref().map(|e| e.source()).flatten()
}
}
#[derive(Debug)]
pub enum ErrorKind {
Client(ClientErrorKind),
Server(ServerErrorKind)
}
impl From<ClientErrorKind> for ErrorKind {
fn from(k: ClientErrorKind) -> Self {
Self::Client(k)
}
}
impl From<ServerErrorKind> for ErrorKind {
fn from(k: ServerErrorKind) -> Self {
Self::Server(k)
}
}
macro_rules! error_kind {
($name:ident, $($kind:ident),*) => (
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum $name {
$($kind),*
}
impl From<$name> for StatusCode {
fn from(k: $name) -> Self {
match k {
$($name::$kind => Self::$kind),*
}
}
}
)
}
error_kind!( ClientErrorKind,
BadRequest,
Unauthorized,
PaymentRequired,
Forbidden,
NotFound,
MethodNotAllowed,
NotAcceptable,
ProxyAuthenticationRequired,
RequestTimeout,
Conflict,
Gone,
LengthRequired,
PreconditionFailed,
RequestEntityTooLarge,
RequestURITooLarge,
UnsupportedMediaType,
RequestedRangeNotSatisfiable,
ExpectationFailed
);
impl ClientErrorKind {
pub fn from_io(error: &io::Error) -> Self {
if let Some(src) = error.get_ref() {
if src.is::<SizeLimitReached>() {
return Self::RequestEntityTooLarge
}
}
use io::ErrorKind::*;
match error.kind() {
NotFound => Self::NotFound,
PermissionDenied => Self::Unauthorized,
AlreadyExists => Self::Conflict,
InvalidInput |
InvalidData |
Other => Self::BadRequest,
TimedOut => Self::RequestTimeout,
_ => Self::ExpectationFailed
}
}
}
error_kind!( ServerErrorKind,
InternalServerError,
NotImplemented,
BadGateway,
ServiceUnavailable,
GatewayTimeout
);