pub struct BoxedError2 { /* private fields */ }Expand description
Type-erased error with automatic backtrace tracking.
BoxedError2 provides anyhow-like ergonomics with type erasure,
allowing you to work with errors without specifying concrete types.
§Features
- Type Erasure: Store any error implementing
Error2orstd::error::Error - Downcasting: Retrieve the original error type
- Backtrace: Access complete error chain and locations
§Conversion Control
Use Via* types to control how errors are wrapped:
use error2::prelude::*;
fn example() -> Result<(), BoxedError2> {
// Wrap std::error::Error
std::fs::read_to_string("file.txt").context(ViaStd)?;
Ok(())
}See ViaRoot, ViaStd, and ViaErr2 for details.
§Downcasting
Retrieve the original error type:
use std::io;
use error2::{kind::ErrorKind, prelude::*};
let err = std::io::Error::from(std::io::ErrorKind::NotFound);
let boxed = BoxedError2::from_std(err);
match boxed.downcast_ref::<io::Error>() {
Some(ErrorKind::Std { source, .. }) => {
println!("IO error: {}", source);
}
_ => {}
}Implementations§
Source§impl BoxedError2
impl BoxedError2
Sourcepub fn is<T: Error + 'static>(&self) -> bool
pub fn is<T: Error + 'static>(&self) -> bool
Checks if the boxed error contains an error of type T.
Sourcepub fn downcast_ref<T: Error + 'static>(
&self,
) -> Option<ErrorKind<&T, &Backtrace>>
pub fn downcast_ref<T: Error + 'static>( &self, ) -> Option<ErrorKind<&T, &Backtrace>>
Attempts to downcast to a reference of type T.
Returns Some(ErrorKind) if the error is of type T.
Source§impl BoxedError2
impl BoxedError2
Sourcepub fn from_root<R>(root: R) -> BoxedError2
pub fn from_root<R>(root: R) -> BoxedError2
Creates a BoxedError2 from a root error message.
Use for errors that don’t wrap other errors.
Sourcepub fn from_root_with_location<R>(root: R, location: Location) -> BoxedError2
pub fn from_root_with_location<R>(root: R, location: Location) -> BoxedError2
Creates a root error with explicit location.
Sourcepub fn from_std<T>(source: T) -> BoxedError2
pub fn from_std<T>(source: T) -> BoxedError2
Creates a BoxedError2 from a std::error::Error.
Wraps standard library or third-party errors.
Sourcepub fn from_std_with_location<T>(source: T, location: Location) -> BoxedError2
pub fn from_std_with_location<T>(source: T, location: Location) -> BoxedError2
Creates from std::error::Error with explicit location.
Sourcepub fn from_err2<T>(source: T) -> BoxedError2
pub fn from_err2<T>(source: T) -> BoxedError2
Creates a BoxedError2 from an Error2 type.
Preserves the original error’s backtrace.
Sourcepub fn from_err2_with_location<T>(source: T, location: Location) -> BoxedError2
pub fn from_err2_with_location<T>(source: T, location: Location) -> BoxedError2
Creates from Error2 with explicit location.