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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use core::fmt::{self, Display, Debug};
use {Causes, Fail};
use backtrace::Backtrace;
use context::Context;
use compat::Compat;
#[cfg(feature = "std")]
use box_std::BoxStd;
#[cfg_attr(feature = "small-error", path = "./error_impl_small.rs")]
mod error_impl;
use self::error_impl::ErrorImpl;
#[cfg(feature = "std")]
use std::error::Error as StdError;
pub struct Error {
imp: ErrorImpl,
}
impl<F: Fail> From<F> for Error {
fn from(failure: F) -> Error {
Error {
imp: ErrorImpl::from(failure)
}
}
}
impl Error {
#[cfg(feature = "std")]
pub fn from_boxed_compat(err: Box<StdError + Sync + Send + 'static>) -> Error {
Error::from(BoxStd(err))
}
pub fn as_fail(&self) -> &Fail {
self.imp.failure()
}
pub fn name(&self) -> Option<&str> {
self.as_fail().name()
}
#[deprecated(since = "0.1.2", note = "please use 'as_fail()' method instead")]
pub fn cause(&self) -> &Fail {
self.as_fail()
}
pub fn backtrace(&self) -> &Backtrace {
self.imp.failure().backtrace().unwrap_or(&self.imp.backtrace())
}
pub fn context<D: Display + Send + Sync + 'static>(self, context: D) -> Context<D> {
Context::with_err(context, self)
}
pub fn compat(self) -> Compat<Error> {
Compat { error: self }
}
pub fn downcast<T: Fail>(self) -> Result<T, Error> {
self.imp.downcast().map_err(|imp| Error { imp })
}
pub fn find_root_cause(&self) -> &Fail {
self.as_fail().find_root_cause()
}
pub fn iter_causes(&self) -> Causes {
self.as_fail().iter_causes()
}
pub fn iter_chain(&self) -> Causes {
self.as_fail().iter_chain()
}
pub fn downcast_ref<T: Fail>(&self) -> Option<&T> {
self.imp.failure().downcast_ref()
}
pub fn downcast_mut<T: Fail>(&mut self) -> Option<&mut T> {
self.imp.failure_mut().downcast_mut()
}
#[deprecated(since = "0.1.2", note = "please use the 'find_root_cause()' method instead")]
pub fn root_cause(&self) -> &Fail {
::find_root_cause(self.as_fail())
}
#[deprecated(since = "0.1.2", note = "please use the 'iter_chain()' method instead")]
pub fn causes(&self) -> Causes {
Causes { fail: Some(self.as_fail()) }
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.imp.failure(), f)
}
}
impl Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let backtrace = self.imp.backtrace();
if backtrace.is_none() {
Debug::fmt(&self.imp.failure(), f)
} else {
write!(f, "{:?}\n\n{:?}", &self.imp.failure(), backtrace)
}
}
}
impl AsRef<Fail> for Error {
fn as_ref(&self) -> &Fail {
self.as_fail()
}
}
#[cfg(test)]
mod test {
use std::io;
use super::Error;
fn assert_just_data<T: Send + Sync + 'static>() { }
#[test]
fn assert_error_is_just_data() {
assert_just_data::<Error>();
}
#[test]
fn methods_seem_to_work() {
let io_error: io::Error = io::Error::new(io::ErrorKind::NotFound, "test");
let error: Error = io::Error::new(io::ErrorKind::NotFound, "test").into();
assert!(error.downcast_ref::<io::Error>().is_some());
let _: ::Backtrace = *error.backtrace();
assert_eq!(format!("{:?}", io_error), format!("{:?}", error));
assert_eq!(format!("{}", io_error), format!("{}", error));
drop(error);
assert!(true);
}
#[test]
fn downcast_can_be_used() {
let mut error: Error = io::Error::new(io::ErrorKind::NotFound, "test").into();
{
let real_io_error_ref = error.downcast_ref::<io::Error>().unwrap();
assert_eq!(real_io_error_ref.to_string(), "test");
}
{
let real_io_error_mut = error.downcast_mut::<io::Error>().unwrap();
assert_eq!(real_io_error_mut.to_string(), "test");
}
let real_io_error = error.downcast::<io::Error>().unwrap();
assert_eq!(real_io_error.to_string(), "test");
}
}