pub struct TracedError<E> { /* private fields */ }
Expand description
A wrapper type for Error
s 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(),
}
}
}
Implementations§
Source§impl<E> TracedError<E>
impl<E> TracedError<E>
Sourcepub fn new(error: E, span_trace: SpanTrace) -> Self
pub fn new(error: E, span_trace: SpanTrace) -> Self
Construct a TracedError given both the SpanTrace and the inner Error
Sourcepub fn map<F, O>(self, op: O) -> TracedError<F>
pub fn map<F, O>(self, op: O) -> TracedError<F>
Convert the inner error type of a TracedError
while preserving the
attached SpanTrace
.
§Examples
use tracing_error::TracedError;
let err: TracedError<InnerError> = InnerError.into();
let err: TracedError<OuterError> = err.map(|inner| OuterError(inner));
Sourcepub fn err_into<F>(self) -> TracedError<F>
pub fn err_into<F>(self) -> TracedError<F>
Convert the inner error type of a TracedError
using the inner error’s Into
implementation, while preserving the attached SpanTrace
.
§Examples
use tracing_error::TracedError;
impl From<InnerError> for OuterError {
fn from(inner: InnerError) -> Self {
Self(inner)
}
}
let err: TracedError<InnerError> = InnerError.into();
let err: TracedError<OuterError> = err.err_into();
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)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
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
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>
traced-error
only.Source§fn in_current_span(self) -> <E as InstrumentError>::Instrumented
fn in_current_span(self) -> <E as InstrumentError>::Instrumented
traced-error
only.