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 toErrorfor type-erased errors (the common case withhandle!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>
impl<E> Handled<E>
Sourcepub fn new(source: E) -> Selfwhere
E: Display,
pub fn new(source: E) -> Selfwhere
E: Display,
Create a new Handled wrapper around an error. Message is computed lazily on first access.
Sourcepub fn message(&self) -> &strwhere
E: Display,
pub fn message(&self) -> &strwhere
E: Display,
Get the error message, computing it lazily on first access.
Sourcepub fn source_ref(&self) -> &E
pub fn source_ref(&self) -> &E
Get the underlying error source.
Sourcepub fn into_source(self) -> E
pub fn into_source(self) -> E
Consume and return the underlying error.
Sourcepub fn frames(&self) -> impl Iterator<Item = FrameView<'_>>
pub fn frames(&self) -> impl Iterator<Item = FrameView<'_>>
Iterate over frames in the trace. Combines locations with their optional contexts.
Sourcepub fn context_count(&self) -> usize
pub fn context_count(&self) -> usize
Number of context entries (frames with messages/attachments).
Source§impl Handled<Error>
impl Handled<Error>
Sourcepub fn wrap<E>(e: E) -> Self
pub fn wrap<E>(e: E) -> Self
Wrap any error, avoiding double-boxing if already Handled. Message is computed lazily on first access.
Sourcepub fn wrap_box(e: Box<dyn StdError + Send + Sync + 'static>) -> Self
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.
Sourcepub fn wrap_erased(e: Error) -> Self
pub fn wrap_erased(e: Error) -> Self
Wrap an Error directly. Message is computed lazily on first access.
Sourcepub fn msg(message: impl Into<String>) -> Self
pub fn msg(message: impl Into<String>) -> Self
Create from a message string. Message is pre-initialized since we already have it.
Sourcepub fn downcast_ref<T: StdError + 'static>(&self) -> Option<&T>
pub fn downcast_ref<T: StdError + 'static>(&self) -> Option<&T>
Try to downcast to a specific error type.
Sourcepub fn downcast<T: StdError + 'static>(self) -> Result<T, Self>
pub fn downcast<T: StdError + 'static>(self) -> Result<T, Self>
Try to downcast and consume the error.
Sourcepub fn chain_any<T: StdError + 'static>(&self) -> Option<&T>
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());
}
}Sourcepub fn chain_all<T: StdError + 'static>(&self) -> Vec<&T>
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 Error for Handled<Error>
Available on crate feature std only.
impl Error for Handled<Error>
std only.Source§fn source(&self) -> Option<&(dyn StdError + 'static)>
fn source(&self) -> Option<&(dyn StdError + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<Box<dyn Display + Sync + Send>> for Handled<Error>
Available on crate feature std only.
impl From<Box<dyn Display + Sync + Send>> for Handled<Error>
std only.Source§impl From<Box<dyn Error + Sync + Send>> for Handled<Error>
Available on crate feature std only.
impl From<Box<dyn Error + Sync + Send>> for Handled<Error>
std only.Source§impl From<FromUtf8Error> for Handled<Error>
Available on crate feature std only.
impl From<FromUtf8Error> for Handled<Error>
std only.Source§fn from(e: FromUtf8Error) -> Self
fn from(e: FromUtf8Error) -> Self
Source§impl From<ParseFloatError> for Handled<Error>
Available on crate feature std only.
impl From<ParseFloatError> for Handled<Error>
std only.Source§fn from(e: ParseFloatError) -> Self
fn from(e: ParseFloatError) -> Self
Source§impl From<ParseIntError> for Handled<Error>
Available on crate feature std only.
impl From<ParseIntError> for Handled<Error>
std only.