ErrorExt

Trait ErrorExt 

Source
pub trait ErrorExt: StdError {
    // Required methods
    fn message(&self) -> String;
    fn backtrace(&self) -> &Backtrace;

    // Provided methods
    fn has_backtrace(&self) -> bool { ... }
    fn find_source<T: StdError + 'static>(&self) -> Option<&T> { ... }
    fn find_source_with<T: StdError + 'static>(
        &self,
        search: impl Fn(&T) -> bool,
    ) -> Option<&T> { ... }
}
Expand description

Extension trait providing additional functionality for ohno error types.

This trait is automatically implemented by #[derive(Error)] and #[ohno::error]. It provides convenient methods for error handling, backtrace access, and error chain traversal.

§Examples

use ohno::ErrorExt;

#[ohno::error]
#[from(std::io::Error)]
struct NetworkError;

#[ohno::error]
#[from(NetworkError)]
struct ServiceError;

let io_error = std::io::Error::new(std::io::ErrorKind::ConnectionAborted, "connection aborted");
let network_err = NetworkError::from(io_error);
let service_err = ServiceError::from(network_err);
let io_error = service_err.find_source::<std::io::Error>().unwrap();

Required Methods§

Source

fn message(&self) -> String

Returns the formatted error message without backtrace.

Provides a clean, user-friendly error message excluding backtrace information. Ideal for user interfaces, logs, or when backtrace details are not needed.

Source

fn backtrace(&self) -> &Backtrace

Returns a reference to the captured backtrace.

Provides access to the stack trace captured when the error was created. Use has_backtrace() to check if backtrace was captured.

§Backtrace Capture

Controlled by environment variables:

  • RUST_BACKTRACE=1 enables basic backtrace
  • RUST_BACKTRACE=full enables full backtrace with all frames

Provided Methods§

Source

fn has_backtrace(&self) -> bool

Returns true if the error has a captured backtrace.

Convenience method equivalent to checking if backtrace status is Captured.

Source

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

Finds the first source error of the specified type in the error chain.

Walks through the error’s source chain and returns the first error that matches type T. Only searches the source chain, not the current error itself.

Source

fn find_source_with<T: StdError + 'static>( &self, search: impl Fn(&T) -> bool, ) -> Option<&T>

Finds the first source error of the specified type that matches the given predicate.

Walks through the error’s source chain and returns the first error that matches type T and satisfies the provided search predicate. Only searches the source chain, not the current error itself.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§