pub struct TracedError<E> { /* private fields */ }Expand description
A wrapper type for Errors that bundles a SpanTrace with an inner Error
type.
This type is a good match for the error-kind pattern where you have an error type with an inner enum of error variants and you would like to capture a span trace that can be extracted during printing without formatting the span trace as part of your display impl.
An example of implementing an error type for a library using TracedError
might look like this
ⓘ
#[derive(Debug, thiserror::Error)]
enum Kind {
    // ...
}
#[derive(Debug)]
pub struct Error {
    source: TracedError<Kind>,
    backtrace: Backtrace,
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source.source()
    }
    fn backtrace(&self) -> Option<&Backtrace> {
        Some(&self.backtrace)
    }
}
impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.source, fmt)
    }
}
impl<E> From<E> for Error
where
    Kind: From<E>,
{
    fn from(source: E) -> Self {
        Self {
            source: Kind::from(source).into(),
            backtrace: Backtrace::capture(),
        }
    }
}Trait Implementations§
Source§impl<E> Debug for TracedError<E>where
    E: Error,
 
impl<E> Debug for TracedError<E>where
    E: Error,
Source§impl<E> Display for TracedError<E>where
    E: Error,
 
impl<E> Display for TracedError<E>where
    E: Error,
Source§impl<E> Error for TracedError<E>where
    E: Error + 'static,
 
impl<E> Error for TracedError<E>where
    E: Error + 'static,
Source§fn source<'a>(&'a self) -> Option<&'a (dyn Error + 'static)>
 
fn source<'a>(&'a self) -> Option<&'a (dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
 
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Auto Trait Implementations§
impl<E> Freeze for TracedError<E>where
    E: Freeze,
impl<E> !RefUnwindSafe for TracedError<E>
impl<E> Send for TracedError<E>where
    E: Send,
impl<E> Sync for TracedError<E>where
    E: Sync,
impl<E> Unpin for TracedError<E>where
    E: Unpin,
impl<E> !UnwindSafe for TracedError<E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
 
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
 
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
 
fn in_current_span(self) -> Instrumented<Self>
Source§impl<E> InstrumentError for Ewhere
    TracedError<E>: From<E>,
 
impl<E> InstrumentError for Ewhere
    TracedError<E>: From<E>,
Source§type Instrumented = TracedError<E>
 
type Instrumented = TracedError<E>
The type of the wrapped error after instrumentation
Source§fn in_current_span(self) -> <E as InstrumentError>::Instrumented
 
fn in_current_span(self) -> <E as InstrumentError>::Instrumented
Instrument an Error by bundling it with a SpanTrace Read more