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
use std::error::Error as StdError;
use std::fmt::{self, Debug, Display};
use crate::StatusCode;
pub type Result<T> = std::result::Result<T, Error>;
pub struct Error {
error: anyhow::Error,
status: crate::StatusCode,
}
impl Error {
pub fn new<E>(status: StatusCode, error: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
Self {
status,
error: anyhow::Error::new(error),
}
}
pub fn from_str<M>(status: StatusCode, msg: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
{
Self {
status,
error: anyhow::Error::msg(msg),
}
}
pub(crate) fn new_adhoc<M>(message: M) -> Error
where
M: Display + Debug + Send + Sync + 'static,
{
Self::from_str(StatusCode::InternalServerError, message)
}
pub fn status(&self) -> StatusCode {
self.status
}
pub fn set_status(&mut self, status: StatusCode) {
self.status = status;
}
#[cfg(backtrace)]
pub fn backtrace(&self) -> &std::backtrace::Backtrace {
self.error.downcast_ref::<E>()
}
pub fn downcast<E>(self) -> std::result::Result<E, Self>
where
E: Display + Debug + Send + Sync + 'static,
{
if self.error.downcast_ref::<E>().is_some() {
Ok(self.error.downcast().unwrap())
} else {
Err(self)
}
}
pub fn downcast_ref<E>(&self) -> Option<&E>
where
E: Display + Debug + Send + Sync + 'static,
{
self.error.downcast_ref::<E>()
}
pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
where
E: Display + Debug + Send + Sync + 'static,
{
self.error.downcast_mut::<E>()
}
}
impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.error)
}
}
impl Debug for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.error)
}
}
impl<E> From<E> for Error
where
E: StdError + Send + Sync + 'static,
{
fn from(error: E) -> Self {
Self {
error: anyhow::Error::new(error),
status: StatusCode::InternalServerError,
}
}
}
impl AsRef<dyn StdError + Send + Sync> for Error {
fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
self.error.as_ref()
}
}
impl AsRef<StatusCode> for Error {
fn as_ref(&self) -> &StatusCode {
&self.status
}
}
impl AsMut<StatusCode> for Error {
fn as_mut(&mut self) -> &mut StatusCode {
&mut self.status
}
}
impl AsRef<dyn StdError> for Error {
fn as_ref(&self) -> &(dyn StdError + 'static) {
self.error.as_ref()
}
}
impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
fn from(error: Error) -> Self {
error.error.into()
}
}
impl From<Error> for Box<dyn StdError + 'static> {
fn from(error: Error) -> Self {
Box::<dyn StdError + Send + Sync>::from(error.error)
}
}