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
#![deny(missing_docs)]
#![deny(unsafe_code)]
#![warn(missing_doc_code_examples)]
use std::panic::PanicInfo;
use failure::{Error, Fail};
use sentry_backtrace::parse_stacktrace;
use sentry_core::parse_type_from_debug;
use sentry_core::protocol::{Event, Exception, Level};
use sentry_core::types::Uuid;
use sentry_core::{ClientOptions, Hub, Integration};
#[derive(Default)]
pub struct FailureIntegration;
impl FailureIntegration {
pub fn new() -> Self {
Self::default()
}
}
impl Integration for FailureIntegration {
fn name(&self) -> &'static str {
"failure"
}
fn setup(&self, cfg: &mut ClientOptions) {
cfg.in_app_exclude.push("failure::");
cfg.extra_border_frames.extend_from_slice(&[
"failure::error_message::err_msg",
"failure::backtrace::Backtrace::new",
"failure::backtrace::internal::InternalBacktrace::new",
"failure::Fail::context",
]);
}
}
pub fn panic_extractor(info: &PanicInfo<'_>) -> Option<Event<'static>> {
let error = info.payload().downcast_ref::<Error>()?;
Some(Event {
level: Level::Fatal,
..event_from_error(error)
})
}
pub fn exception_from_single_fail<F: Fail + ?Sized>(
f: &F,
bt: Option<&failure::Backtrace>,
) -> Exception {
let dbg = format!("{:?}", f);
Exception {
ty: parse_type_from_debug(&dbg).to_owned(),
value: Some(f.to_string()),
stacktrace: bt
.map(|bt| format!("{:#?}", bt))
.and_then(|x| parse_stacktrace(&x)),
..Default::default()
}
}
pub fn event_from_error(err: &failure::Error) -> Event<'static> {
let mut exceptions: Vec<_> = err
.iter_chain()
.enumerate()
.map(|(idx, cause)| {
let bt = match cause.backtrace() {
Some(bt) => Some(bt),
None if idx == 0 => Some(err.backtrace()),
None => None,
};
exception_from_single_fail(cause, bt)
})
.collect();
exceptions.reverse();
Event {
exception: exceptions.into(),
level: Level::Error,
..Default::default()
}
}
pub fn event_from_fail<F: Fail + ?Sized>(fail: &F) -> Event<'static> {
let mut exceptions = vec![exception_from_single_fail(fail, fail.backtrace())];
let mut ptr: Option<&dyn Fail> = None;
while let Some(cause) = ptr.map(Fail::cause).unwrap_or_else(|| fail.cause()) {
exceptions.push(exception_from_single_fail(cause, cause.backtrace()));
ptr = Some(cause);
}
exceptions.reverse();
Event {
exception: exceptions.into(),
level: Level::Error,
..Default::default()
}
}
pub fn capture_error(err: &Error) -> Uuid {
Hub::with_active(|hub| FailureHubExt::capture_error(hub.as_ref(), err))
}
pub fn capture_fail<F: Fail + ?Sized>(fail: &F) -> Uuid {
Hub::with_active(|hub| hub.capture_fail(fail))
}
pub trait FailureHubExt {
fn capture_error(&self, err: &Error) -> Uuid;
fn capture_fail<F: Fail + ?Sized>(&self, fail: &F) -> Uuid;
}
impl FailureHubExt for Hub {
fn capture_error(&self, err: &Error) -> Uuid {
self.capture_event(event_from_error(err))
}
fn capture_fail<F: Fail + ?Sized>(&self, fail: &F) -> Uuid {
self.capture_event(event_from_fail(fail))
}
}
pub trait FailureResultExt {
type Value;
fn fallible_unwrap(self) -> Self::Value;
}
impl<T, E> FailureResultExt for Result<T, E>
where
E: Into<Error>,
{
type Value = T;
fn fallible_unwrap(self) -> Self::Value {
match self {
Ok(v) => v,
Err(e) => {
let e: Error = e.into();
panic!(e)
}
}
}
}