Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit a7fadef

Browse files
committed
Always generate Error::backtrace.
1 parent fc117ef commit a7fadef

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/error_chain.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ macro_rules! error_chain_processed {
8686
pub fn iter(&self) -> $crate::ErrorChainIter {
8787
$crate::ErrorChainIter(Some(self))
8888
}
89+
90+
/// Returns the backtrace associated with this error.
91+
pub fn backtrace(&self) -> Option<&$crate::Backtrace> {
92+
self.state.backtrace()
93+
}
8994
}
9095

9196
impl ::std::error::Error for $error_name {
@@ -308,13 +313,6 @@ macro_rules! impl_error {
308313
($error_name: ident
309314
$error_kind_name: ident
310315
$([$link_error_path: path, $(#[$meta_links: meta])*])*) => {
311-
impl $error_name {
312-
/// Returns the backtrace associated with this error.
313-
pub fn backtrace(&self) -> Option<&$crate::Backtrace> {
314-
self.state.backtrace.as_ref().map(|v| &**v)
315-
}
316-
}
317-
318316
impl $crate::ChainedError for $error_name {
319317
type ErrorKind = $error_kind_name;
320318

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ use std::sync::Arc;
261261

262262
#[cfg(feature = "backtrace")]
263263
pub use backtrace::Backtrace;
264+
#[cfg(not(feature = "backtrace"))]
265+
/// Dummy type used when the `backtrace` feature is disabled.
266+
pub type Backtrace = ();
264267

265268
#[macro_use]
266269
mod quick_error;
@@ -371,6 +374,17 @@ impl Default for State {
371374
}
372375
}
373376

377+
impl State {
378+
/// Returns the inner backtrace if present.
379+
pub fn backtrace(&self) -> Option<&Backtrace> {
380+
#[cfg(feature = "backtrace")]
381+
let b = self.backtrace.as_ref().map(|v| &**v);
382+
#[cfg(not(feature = "backtrace"))]
383+
let b = None;
384+
b
385+
}
386+
}
387+
374388
/// This modules show an example of code generated by the macro. IT MUST NOT BE
375389
/// USED OUTSIDE THIS CRATE.
376390
///

0 commit comments

Comments
 (0)