Skip to content

Commit

Permalink
feat: Make anyhow backtrace feature optional (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbzweihander authored Jun 22, 2021
1 parent e2311e6 commit 81e133f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 4 additions & 1 deletion sentry-anyhow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ Sentry integration for anyhow.
"""
edition = "2018"

[features]
backtrace = ["anyhow/backtrace"]

[dependencies]
sentry-backtrace = { version = "0.22.0", path = "../sentry-backtrace" }
sentry-core = { version = "0.22.0", path = "../sentry-core" }
anyhow = { version = "1.0.39", features = ["backtrace"] }
anyhow = "1.0.39"

[dev-dependencies]
sentry = { version = "0.22.0", path = "../sentry", default-features = false, features = ["test"] }
21 changes: 14 additions & 7 deletions sentry-anyhow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,25 @@ pub trait AnyhowHubExt {
impl AnyhowHubExt for Hub {
fn capture_anyhow(&self, anyhow_error: &anyhow::Error) -> Uuid {
let dyn_err: &dyn std::error::Error = anyhow_error.as_ref();
let mut event = sentry_core::event_from_error(dyn_err);

// exception records are sorted in reverse
if let Some(exc) = event.exception.iter_mut().last() {
let backtrace = anyhow_error.backtrace();
exc.stacktrace = sentry_backtrace::parse_stacktrace(&format!("{:#}", backtrace));
}
#[cfg(feature = "backtrace")]
{
let mut event = sentry_core::event_from_error(dyn_err);

// exception records are sorted in reverse
if let Some(exc) = event.exception.iter_mut().last() {
let backtrace = anyhow_error.backtrace();
exc.stacktrace = sentry_backtrace::parse_stacktrace(&format!("{:#}", backtrace));
}

self.capture_event(event)
self.capture_event(event)
}
#[cfg(not(feature = "backtrace"))]
self.capture_error(dyn_err)
}
}

#[cfg(all(feature = "backtrace", test))]
#[test]
fn test_has_backtrace() {
std::env::set_var("RUST_BACKTRACE", "1");
Expand Down

0 comments on commit 81e133f

Please sign in to comment.