Skip to content

Commit d7f5f2b

Browse files
authored
fix: Honor the attach_stacktrace option when capturing errors (#340)
1 parent cd14871 commit d7f5f2b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- The minium supported Rust version was bumped to **1.46.0** due to requirements from dependencies.
88

9+
**Fixes**:
10+
11+
- Honor the `attach_stacktrace` option correctly when capturing errors.
12+
913
## 0.22.0
1014

1115
**Breaking Changes**:

sentry-backtrace/src/integration.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Integration for AttachStacktraceIntegration {
7373
mut event: Event<'static>,
7474
options: &ClientOptions,
7575
) -> Option<Event<'static>> {
76-
if options.attach_stacktrace && event.exception.is_empty() {
76+
if options.attach_stacktrace && !has_stacktrace(&event) {
7777
let thread = current_thread(true);
7878
if thread.stacktrace.is_some() {
7979
event.threads.values.push(thread);
@@ -83,6 +83,12 @@ impl Integration for AttachStacktraceIntegration {
8383
}
8484
}
8585

86+
fn has_stacktrace(event: &Event) -> bool {
87+
event.stacktrace.is_some()
88+
|| event.exception.iter().any(|exc| exc.stacktrace.is_some())
89+
|| event.threads.iter().any(|thrd| thrd.stacktrace.is_some())
90+
}
91+
8692
/// Captures information about the current thread.
8793
///
8894
/// If `with_stack` is set to `true` the current stacktrace is

sentry/tests/test_basic.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,38 @@ fn test_reentrant_configure_scope() {
139139
// well, the "outer" `configure_scope` wins
140140
assert_eq!(events[0].tags["which_scope"], "scope1");
141141
}
142+
143+
#[test]
144+
fn test_attached_stacktrace() {
145+
use log_ as log;
146+
147+
let logger = sentry_log::SentryLogger::new();
148+
149+
log::set_boxed_logger(Box::new(logger))
150+
.map(|()| log::set_max_level(log::LevelFilter::Info))
151+
.unwrap();
152+
153+
let options = sentry::apply_defaults(sentry::ClientOptions {
154+
attach_stacktrace: true,
155+
..Default::default()
156+
});
157+
let events = sentry::test::with_captured_events_options(
158+
|| {
159+
let error = "thisisnotanumber".parse::<u32>().unwrap_err();
160+
sentry::capture_error(&error);
161+
162+
sentry::capture_message("some kind of message", sentry::Level::Info);
163+
164+
log::error!("Shit's on fire yo");
165+
},
166+
options,
167+
);
168+
169+
assert_eq!(events.len(), 3);
170+
171+
let stacktraces: Vec<_> = events
172+
.into_iter()
173+
.flat_map(|ev| ev.threads.into_iter().filter_map(|thrd| thrd.stacktrace))
174+
.collect();
175+
assert_eq!(stacktraces.len(), 3);
176+
}

0 commit comments

Comments
 (0)