pub struct FrozenTrace {
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 _, FrozenTrace};
#[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!(
FrozenTrace::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 FrozenTrace
impl FrozenTrace
Sourcepub fn new(err: impl Error) -> Self
pub fn new(err: impl Error) -> Self
Builds a new FrozenTrace from the given Error.
§Arguments
err: The error to walk and to freeze the errors of by serializing the messages.
§Returns
A new FrozenTrace that itself implements Error again.
§Example
See FrozenTrace itself for an example of how to use it, or see ErrorTrace::freeze().
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 _, FrozenTrace};
let trace = FrozenTrace::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 Clone for FrozenTrace
impl Clone for FrozenTrace
Source§fn clone(&self) -> FrozenTrace
fn clone(&self) -> FrozenTrace
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FrozenTrace
impl Debug for FrozenTrace
Source§impl<'de> Deserialize<'de> for FrozenTrace
impl<'de> Deserialize<'de> for FrozenTrace
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for FrozenTrace
impl Display for FrozenTrace
Source§impl Error for FrozenTrace
impl Error for FrozenTrace
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 FrozenTrace
impl RefUnwindSafe for FrozenTrace
impl Send for FrozenTrace
impl Sync for FrozenTrace
impl Unpin for FrozenTrace
impl UnwindSafe for FrozenTrace
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ErrorTrace for T
impl<T> ErrorTrace for T
Source§fn freeze(&self) -> FrozenTrace
fn freeze(&self) -> FrozenTrace
“Freezes” the trace of this error. Read more
Source§fn trace(&self) -> ErrorTraceFormatter<'_, '_, '_>
fn trace(&self) -> ErrorTraceFormatter<'_, '_, '_>
Source§fn trace_colored(&self) -> ErrorTraceColorFormatter<'_, '_, '_>
fn trace_colored(&self) -> ErrorTraceColorFormatter<'_, '_, '_>
Available on crate feature
colors only.