aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/error.rs b/src/error.rs
index 0971146..9f6ce8c 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,6 +5,8 @@ use crate::ptr::Mut;
use crate::ptr::{Own, Ref};
use crate::{Error, StdError};
use alloc::boxed::Box;
+#[cfg(backtrace)]
+use core::any::Demand;
use core::any::TypeId;
use core::fmt::{self, Debug, Display};
use core::mem::ManuallyDrop;
@@ -31,7 +33,7 @@ impl Error {
where
E: StdError + Send + Sync + 'static,
{
- let backtrace = backtrace_if_absent!(error);
+ let backtrace = backtrace_if_absent!(&error);
Error::from_std(error, backtrace)
}
@@ -522,6 +524,18 @@ impl Error {
}
}
+#[cfg(backtrace)]
+impl std::any::Provider for Error {
+ // Called by thiserror when you have `#[source] anyhow::Error`. This provide
+ // implementation includes the anyhow::Error's Backtrace if any, unlike
+ // deref'ing to dyn Error where the provide implementation would include
+ // only the original error's Backtrace from before it got wrapped into an
+ // anyhow::Error.
+ fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
+ unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }
+ }
+}
+
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
impl<E> From<E> for Error
@@ -530,7 +544,7 @@ where
{
#[cold]
fn from(error: E) -> Self {
- let backtrace = backtrace_if_absent!(error);
+ let backtrace = backtrace_if_absent!(&error);
Error::from_std(error, backtrace)
}
}
@@ -886,13 +900,21 @@ impl ErrorImpl {
.as_ref()
.or_else(|| {
#[cfg(backtrace)]
- return Self::error(this).backtrace();
- #[cfg(all(not(backtrace), feature = "backtrace"))]
+ return Self::error(this).request_ref::<Backtrace>();
+ #[cfg(not(backtrace))]
return (vtable(this.ptr).object_backtrace)(this);
})
.expect("backtrace capture failed")
}
+ #[cfg(backtrace)]
+ unsafe fn provide<'a>(this: Ref<'a, Self>, demand: &mut Demand<'a>) {
+ if let Some(backtrace) = &this.deref().backtrace {
+ demand.provide_ref(backtrace);
+ }
+ Self::error(this).provide(demand);
+ }
+
#[cold]
pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
Chain::new(Self::error(this))
@@ -903,14 +925,14 @@ impl<E> StdError for ErrorImpl<E>
where
E: StdError,
{
- #[cfg(backtrace)]
- fn backtrace(&self) -> Option<&Backtrace> {
- Some(unsafe { ErrorImpl::backtrace(self.erase()) })
- }
-
fn source(&self) -> Option<&(dyn StdError + 'static)> {
unsafe { ErrorImpl::error(self.erase()).source() }
}
+
+ #[cfg(backtrace)]
+ fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
+ unsafe { ErrorImpl::provide(self.erase(), demand) }
+ }
}
impl<E> Debug for ErrorImpl<E>