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;
use std::error;
use std::result;
use std::ops::Deref;
use std::borrow::Cow;
use backtrace::Backtrace;
use bson;
use mongodb;
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
pub trait ErrorExt: error::Error {
fn reason(&self) -> Option<&ErrorExt> {
None
}
fn backtrace(&self) -> Option<&Backtrace> {
None
}
fn as_std_error(&self) -> &error::Error;
}
pub trait ResultExt<T>: Sized {
fn chain<M: ErrMsg>(self, message: M) -> Result<T>;
}
pub trait ErrMsg: Sized {
fn into_message(self) -> Cow<'static, str>;
}
pub type Result<T> = result::Result<T, Error>;
impl<T, E> ResultExt<T> for result::Result<T, E> where E: ErrorExt + 'static {
fn chain<M: ErrMsg>(self, message: M) -> Result<T> {
self.map_err(|cause| {
let message = message.into_message();
let backtrace = if cause.backtrace().is_none() {
Some(Backtrace::new())
} else {
None
};
let cause: Option<Box<ErrorExt>> = Some(Box::new(cause));
Error { message, cause, backtrace }
})
}
}
impl ErrMsg for &'static str {
fn into_message(self) -> Cow<'static, str> {
Cow::Borrowed(self)
}
}
impl<F> ErrMsg for F where F: FnOnce() -> String {
fn into_message(self) -> Cow<'static, str> {
Cow::Owned(self())
}
}
#[derive(Debug)]
pub struct Error {
message: Cow<'static, str>,
cause: Option<Box<ErrorExt>>,
backtrace: Option<Backtrace>,
}
impl Error {
pub fn new<S>(message: S) -> Self where S: Into<Cow<'static, str>> {
Error {
message: message.into(),
cause: None,
backtrace: Some(Backtrace::new()),
}
}
pub fn with_cause<S, E>(message: S, cause: E) -> Self
where S: Into<Cow<'static, str>>,
E: ErrorExt + 'static
{
let message = message.into();
let backtrace = if cause.backtrace().is_none() {
Some(Backtrace::new())
} else {
None
};
let cause: Option<Box<ErrorExt>> = Some(Box::new(cause));
Error { message, cause, backtrace }
}
}
impl ErrorExt for Error {
fn reason(&self) -> Option<&ErrorExt> {
self.cause.as_ref().map(Deref::deref)
}
#[cfg_attr(feature = "cargo-clippy", allow(or_fun_call))]
fn backtrace(&self) -> Option<&Backtrace> {
self.reason().and_then(ErrorExt::backtrace).or(self.backtrace.as_ref())
}
fn as_std_error(&self) -> &error::Error {
self
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.message)?;
if let Some(cause) = self.cause.as_ref() {
write!(f, ", caused by: {}", cause)?
}
if let Some(backtrace) = self.backtrace.as_ref() {
write!(f, "; {:#?}", backtrace)?
}
Ok(())
}
}
impl error::Error for Error {
fn description(&self) -> &str {
&self.message
}
fn cause(&self) -> Option<&error::Error> {
self.reason().map(ErrorExt::as_std_error)
}
}
macro_rules! impl_error_type {
($ty:path, $message:expr) => {
impl From<$ty> for Error {
fn from(error: $ty) -> Self {
Self::with_cause($message, error)
}
}
impl ErrorExt for $ty {
fn as_std_error(&self) -> &error::Error {
self
}
}
}
}
impl_error_type! { bson::EncoderError, "BSON encoding error" }
impl_error_type! { bson::DecoderError, "BSON decoding error" }
impl_error_type! { bson::ValueAccessError, "missing or ill-typed BSON value" }
impl_error_type! { mongodb::Error, "MongoDB error" }
impl_error_type! { mongodb::coll::error::WriteException, "MongoDB write exception" }
impl_error_type! { mongodb::coll::error::BulkWriteException, "MongoDB bulk write exception" }