pub struct Trace {
pub message: String,
pub source: Option<Box<Self>>,
}Expand description
A helper type that can be used to “freeze” a trace and then pass it on to a further error.
This is useful in case you’re dealing with errors where you don’t want to propagate the type (e.g., due to lifetimes) but do want to propagate the trace.
§Example
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FResult};
use error_trace::{ErrorTrace as _, Trace};
#[derive(Debug)]
struct SomeError {
msg: String,
}
impl Display for SomeError {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "{}", self.msg) }
}
impl Error for SomeError {}
#[derive(Debug)]
struct HigherError {
msg: String,
child: SomeError,
}
impl Display for HigherError {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult { write!(f, "{}", self.msg) }
}
impl Error for HigherError {
fn source(&self) -> Option<&(dyn 'static + Error)> { Some(&self.child) }
}
let err = HigherError {
msg: "Oh no, something went wrong!".into(),
child: SomeError { msg: "A specific reason".into() },
};
assert_eq!(
Trace::new(err).trace().to_string(),
r#"Oh no, something went wrong!
Caused by:
o A specific reason
"#
);Fields§
§message: StringThe error on this level.
source: Option<Box<Self>>The error on the next level, if any.
Implementations§
Source§impl Trace
impl Trace
Sourcepub fn from_msg(msg: impl Into<String>) -> Self
pub fn from_msg(msg: impl Into<String>) -> Self
Builds a new Trace from a single String.
§Arguments
msg: The (already serialized) message to wrap this trace around.
§Returns
A new Trace that wraps the msg implementing Error.
§Example
use std::error::Error as _;
use error_trace::{ErrorTrace as _, Trace};
let trace = Trace::from_msg("Hello there!");
assert_eq!(trace.trace().to_string(), "Hello there!");Sourcepub fn from_source(msg: impl Into<String>, err: impl Error) -> Self
pub fn from_source(msg: impl Into<String>, err: impl Error) -> Self
Trait Implementations§
Source§impl Error for Trace
impl Error for Trace
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(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 Freeze for Trace
impl RefUnwindSafe for Trace
impl Send for Trace
impl Sync for Trace
impl Unpin for Trace
impl UnwindSafe for Trace
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