Skip to content

Commit 4c188ff

Browse files
committed
make it easier to conditionally use a backtrace
1 parent c29011a commit 4c188ff

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/error.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,19 @@ impl Error {
8585
///
8686
/// [tracking]: https://github.com/rust-lang/rust/issues/53487
8787
#[cfg(backtrace)]
88-
pub fn backtrace(&self) -> &std::backtrace::Backtrace {
89-
self.error.backtrace()
88+
pub fn backtrace(&self) -> Option<&std::backtrace::Backtrace> {
89+
let backtrace = self.error.backtrace();
90+
if let std::backtrace::BacktraceStatus::Captured = backtrace.status() {
91+
Some(backtrace)
92+
} else {
93+
None
94+
}
95+
}
96+
97+
#[cfg(not(backtrace))]
98+
#[allow(missing_docs)]
99+
pub fn backtrace(&self) -> Option<()> {
100+
None
90101
}
91102

92103
/// Attempt to downcast the error object to a concrete type.

0 commit comments

Comments
 (0)