Skip to main content

Handled

Struct Handled 

Source
pub struct Handled<E = Error> { /* private fields */ }
Expand description

Error wrapper that captures context and stack traces for any error.

Handled<E> wraps an error type E while adding:

  • A trace of frames showing where the error propagated
  • Context messages and key-value attachments at each frame

§Type Parameters

  • E - The underlying error type. Defaults to Error for type-erased errors (the common case with handle! macro).

The handle! macro produces Handled<Error> (aliased as Handled), with the concrete error type preserved inside for downcasting via TryCatch.

§Examples

use handle_this::{handle, Handled, Result};

fn read_file(path: &str) -> Result<String> {
    handle! { try { std::fs::read_to_string(path)? } with "reading config" }
}

Implementations§

Source§

impl<E> Handled<E>

Source

pub fn new(source: E) -> Self
where E: Display,

Create a new Handled wrapper around an error. Message is computed lazily on first access.

Source

pub fn message(&self) -> &str
where E: Display,

Get the error message, computing it lazily on first access.

Source

pub fn source_ref(&self) -> &E

Get the underlying error source.

Source

pub fn into_source(self) -> E

Consume and return the underlying error.

Source

pub fn frames(&self) -> impl Iterator<Item = FrameView<'_>>

Iterate over frames in the trace. Combines locations with their optional contexts.

Source

pub fn depth(&self) -> usize

Number of location frames in the trace.

Source

pub fn is_empty(&self) -> bool

Whether the trace is empty.

Source

pub fn context_count(&self) -> usize

Number of context entries (frames with messages/attachments).

Source

pub fn here(self) -> Self

Add a frame at the caller’s location.

Source

pub fn erase(self) -> Handled<Error>
where E: StdError + Send + Sync + 'static,

Convert to a type-erased Handled.

Source

pub fn map_err<F, O>(self, f: F) -> Handled<O>
where F: FnOnce(E) -> O, O: Display,

Map the error type while preserving context.

Source§

impl Handled<Error>

Source

pub fn wrap<E>(e: E) -> Self
where E: StdError + Send + Sync + 'static,

Wrap any error, avoiding double-boxing if already Handled. Message is computed lazily on first access.

Source

pub fn wrap_box(e: Box<dyn StdError + Send + Sync + 'static>) -> Self

Wrap a boxed error into a type-erased Handled. If the boxed error is already a Handled, unwrap it to avoid double-wrapping. Message is computed lazily on first access.

Source

pub fn wrap_erased(e: Error) -> Self

Wrap an Error directly. Message is computed lazily on first access.

Source

pub fn msg(message: impl Into<String>) -> Self

Create from a message string. Message is pre-initialized since we already have it.

Source

pub fn root(&self) -> &(dyn StdError + 'static)

Get the root error as a trait object.

Source

pub fn downcast_ref<T: StdError + 'static>(&self) -> Option<&T>

Try to downcast to a specific error type.

Source

pub fn downcast<T: StdError + 'static>(self) -> Result<T, Self>

Try to downcast and consume the error.

Source

pub fn chain_any<T: StdError + 'static>(&self) -> Option<&T>

Find the first error of type T in the cause chain.

Walks the error chain via std::error::Error::source() and returns a reference to the first error that matches type T.

§Example
use handle_this::{Handled, Result};
use std::io;

fn check_chain(err: &Handled) {
    if let Some(io_err) = err.chain_any::<io::Error>() {
        println!("Found IO error in chain: {:?}", io_err.kind());
    }
}
Source

pub fn chain_all<T: StdError + 'static>(&self) -> Vec<&T>

Find all errors of type T in the cause chain.

Walks the error chain via std::error::Error::source() and collects references to all errors that match type T.

§Example
use handle_this::{Handled, Result};
use std::io;

fn check_all(err: &Handled) {
    let io_errors = err.chain_all::<io::Error>();
    for e in io_errors {
        println!("IO error: {:?}", e.kind());
    }
}

Trait Implementations§

Source§

impl<E: Debug> Debug for Handled<E>

Source§

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

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

impl<E: Display> Display for Handled<E>

Source§

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

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

impl Error for Handled<Error>

Available on crate feature std only.
Source§

fn source(&self) -> Option<&(dyn StdError + '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 From<&str> for Handled<Error>

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<()> for Handled<Error>

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl From<Box<dyn Display + Sync + Send>> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: Box<dyn Display + Send + Sync>) -> Self

Converts to this type from the input type.
Source§

impl From<Box<dyn Error + Sync + Send>> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: Box<dyn StdError + Send + Sync + 'static>) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: Error) -> Self

Converts to this type from the input type.
Source§

impl From<FromUtf8Error> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: FromUtf8Error) -> Self

Converts to this type from the input type.
Source§

impl From<ParseFloatError> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: ParseFloatError) -> Self

Converts to this type from the input type.
Source§

impl From<ParseIntError> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: ParseIntError) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Handled<Error>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Utf8Error> for Handled<Error>

Available on crate feature std only.
Source§

fn from(e: Utf8Error) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Handled<Error>

Source§

fn from(v: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Handled<Error>

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Handled<Error>

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Handled<Error>

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Handled<Error>

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Handled<Error>

Source§

fn from(v: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Handled<Error>

Source§

fn from(v: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Handled<Error>

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Handled<Error>

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Handled<Error>

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Handled<Error>

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Handled<Error>

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<E = Error> !Freeze for Handled<E>

§

impl<E = Error> !RefUnwindSafe for Handled<E>

§

impl<E> Send for Handled<E>
where E: Send,

§

impl<E> Sync for Handled<E>
where E: Sync,

§

impl<E> Unpin for Handled<E>
where E: Unpin,

§

impl<E = Error> !UnwindSafe for Handled<E>

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