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
use std::error::Error;
use crate::protocol::{Event, Exception, Level};
use crate::types::Uuid;
use crate::Hub;
impl Hub {
#[allow(unused)]
pub fn capture_error<E: Error + ?Sized>(&self, error: &E) -> Uuid {
with_client_impl! {{
self.inner.with(|stack| {
let top = stack.top();
if top.client.is_some() {
let event = event_from_error(error);
self.capture_event(event)
} else {
Uuid::nil()
}
})
}}
}
}
#[allow(unused_variables)]
pub fn capture_error<E: Error + ?Sized>(error: &E) -> Uuid {
Hub::with_active(|hub| hub.capture_error(error))
}
pub fn event_from_error<E: Error + ?Sized>(err: &E) -> Event<'static> {
let mut exceptions = vec![exception_from_error(err)];
let mut source = err.source();
while let Some(err) = source {
exceptions.push(exception_from_error(err));
source = err.source();
}
exceptions.reverse();
Event {
exception: exceptions.into(),
level: Level::Error,
..Default::default()
}
}
fn exception_from_error<E: Error + ?Sized>(err: &E) -> Exception {
let dbg = format!("{:?}", err);
Exception {
ty: parse_type_from_debug(&dbg).to_owned(),
value: Some(err.to_string()),
..Default::default()
}
}
pub fn parse_type_from_debug(d: &str) -> &str {
d.split(&[' ', '(', '{', '\r', '\n'][..])
.next()
.unwrap()
.trim()
}
#[test]
fn test_parse_type_from_debug() {
use parse_type_from_debug as parse;
#[derive(Debug)]
struct MyStruct;
let err = format!("{:?}", MyStruct);
assert_eq!(parse(&err), "MyStruct");
let err = format!("{:?}", "NaN".parse::<usize>().unwrap_err());
assert_eq!(parse(&err), "ParseIntError");
let err = format!(
"{:?}",
sentry_types::ParseDsnError::from(sentry_types::ParseProjectIdError::EmptyValue)
);
assert_eq!(parse(&err), "InvalidProjectId");
let err = format!(
"{:#?}",
anyhow::Error::from("NaN".parse::<usize>().unwrap_err())
);
assert_eq!(parse(&err), "ParseIntError");
let err = format!(
"{:?}",
failure::Error::from("NaN".parse::<usize>().unwrap_err())
);
assert_eq!(parse(&err), "ParseIntError");
}