Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add option to include tracing spans in anyhow errors. #397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ std = []
# besides bringing in an unused dependency, as `std::backtrace` is always
# preferred.
backtrace = { version = "0.3.51", optional = true }
tracing = { version = "0.1.7", optional = true }

[dev-dependencies]
futures = { version = "0.3", default-features = false }
Expand Down
24 changes: 24 additions & 0 deletions src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ macro_rules! backtrace {
};
}

#[cfg(feature = "tracing")]
macro_rules! capture_span {
() => {
Some(::tracing::Span::current())
};
}

#[cfg(error_generic_member_access)]
macro_rules! backtrace_if_absent {
($err:expr) => {
Expand Down Expand Up @@ -68,6 +75,23 @@ macro_rules! backtrace_if_absent {
};
}

#[cfg(all(error_generic_member_access, feature = "tracing"))]
macro_rules! span_if_absent {
($err:expr) => {
match core::error::request_ref::<::tracing::Span>($err as &dyn core::error::Error) {
Some(_) => None,
None => capture_span!(),
}
};
}

#[cfg(all(not(error_generic_member_access), feature = "tracing"))]
macro_rules! span_if_absent {
($err:expr) => {
capture_span!()
};
}

#[cfg(all(not(std_backtrace), feature = "backtrace"))]
mod capture {
use alloc::borrow::{Cow, ToOwned as _};
Expand Down
24 changes: 21 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ mod ext {
C: Display + Send + Sync + 'static,
{
let backtrace = backtrace_if_absent!(&self);
Error::from_context(context, self, backtrace)
#[cfg(feature = "tracing")]
let span = span_if_absent!(&self);
Error::from_context(
context,
self,
backtrace,
#[cfg(feature = "tracing")]
span,
)
}
}

Expand Down Expand Up @@ -96,7 +104,12 @@ impl<T> Context<T, Infallible> for Option<T> {
// backtrace.
match self {
Some(ok) => Ok(ok),
None => Err(Error::from_display(context, backtrace!())),
None => Err(Error::from_display(
context,
backtrace!(),
#[cfg(feature = "tracing")]
capture_span!(),
)),
}
}

Expand All @@ -107,7 +120,12 @@ impl<T> Context<T, Infallible> for Option<T> {
{
match self {
Some(ok) => Ok(ok),
None => Err(Error::from_display(context(), backtrace!())),
None => Err(Error::from_display(
context(),
backtrace!(),
#[cfg(feature = "tracing")]
capture_span!(),
)),
}
}
}
Expand Down
Loading
Loading