FrozenTrace

Struct FrozenTrace 

Source
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: String

The error on this level.

§source: Option<Box<Self>>

The error on the next level, if any.

Implementations§

Source§

impl FrozenTrace

Source

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().

Source

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!");
Source

pub fn from_source(msg: impl Into<String>, err: impl Error) -> Self

Builds a new Trace from a message and a source Error.

§Arguments
  • msg: Some toplevel to show as root cause.
  • err: The first error of the trace that causes msg.
§Returns

A new Trace that wraps the msg as error, with err as trace, and that implements Error.

Source

pub fn as_error(&self) -> &(dyn Error + 'static)

Returns this Trace as an Error trait object.

§Returns

A &'static dyn Error which is even static!

Trait Implementations§

Source§

impl Clone for FrozenTrace

Source§

fn clone(&self) -> FrozenTrace

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FrozenTrace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for FrozenTrace

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FResult

Formats the value using the given formatter. Read more
Source§

impl Error for FrozenTrace

Source§

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

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl Serialize for FrozenTrace

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErrorTrace for T
where T: Error + ?Sized,

Source§

fn freeze(&self) -> FrozenTrace

“Freezes” the trace of this error. Read more
Source§

fn trace(&self) -> ErrorTraceFormatter<'_, '_, '_>

Returns a formatter for showing this Error and all its sources. Read more
Source§

fn trace_colored(&self) -> ErrorTraceColorFormatter<'_, '_, '_>

Available on crate feature colors only.
Returns a formatter for showing this Error and all its sources with nice colors. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,