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
pub mod handler;
use failure::{self, Fail};
use http::header::HeaderMap;
use http::StatusCode;
use std::{error, fmt};
pub type CritError = Box<dyn error::Error + Send + Sync + 'static>;
pub type Result<T> = ::std::result::Result<T, Error>;
pub trait HttpError: Fail {
fn status_code(&self) -> StatusCode;
#[allow(unused_variables)]
fn append_headers(&self, h: &mut HeaderMap) {}
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
#[derive(Debug)]
enum ErrorKind {
Boxed(Box<dyn HttpError>),
Concrete(ConcreteHttpError),
Crit(CritError),
}
impl<E> From<E> for Error
where
E: HttpError,
{
fn from(err: E) -> Error {
Error {
kind: ErrorKind::Boxed(Box::new(err)),
}
}
}
impl Error {
pub fn from_failure<E>(cause: E, status: StatusCode) -> Error
where
E: Into<failure::Error>,
{
Error {
kind: ErrorKind::Concrete(ConcreteHttpError {
cause: cause.into(),
status,
}),
}
}
pub fn not_found() -> Error {
Error::from_failure(format_err!("Not Found"), StatusCode::NOT_FOUND)
}
pub fn method_not_allowed() -> Error {
Error::from_failure(format_err!("Method Not Allowed"), StatusCode::METHOD_NOT_ALLOWED)
}
pub fn bad_request<E>(e: E) -> Error
where
E: Into<failure::Error>,
{
Error::from_failure(e, StatusCode::BAD_REQUEST)
}
pub fn internal_server_error<E>(e: E) -> Error
where
E: Into<failure::Error>,
{
Error::from_failure(e, StatusCode::INTERNAL_SERVER_ERROR)
}
pub fn critical<E>(err: E) -> Error
where
E: Into<CritError>,
{
Error {
kind: ErrorKind::Crit(err.into()),
}
}
pub fn is_critical(&self) -> bool {
match self.kind {
ErrorKind::Crit(..) => true,
_ => false,
}
}
pub fn as_http_error(&self) -> Option<&dyn HttpError> {
match self.kind {
ErrorKind::Concrete(ref e) => Some(e),
ErrorKind::Boxed(ref e) => Some(&**e),
ErrorKind::Crit(..) => None,
}
}
pub(crate) fn into_critical(self) -> Option<CritError> {
match self.kind {
ErrorKind::Crit(e) => Some(e),
_ => None,
}
}
#[cfg(test)]
pub(crate) fn status_code(&self) -> Option<StatusCode> {
self.as_http_error().map(|e| e.status_code())
}
}
#[derive(Debug)]
struct ConcreteHttpError {
cause: failure::Error,
status: StatusCode,
}
impl fmt::Display for ConcreteHttpError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.cause, f)
}
}
impl Fail for ConcreteHttpError {}
impl HttpError for ConcreteHttpError {
fn status_code(&self) -> StatusCode {
self.status
}
}