Skip to main content

ErrorContext

Trait ErrorContext 

Source
pub trait ErrorContext<T, E> {
    // Required methods
    fn context(self, context: impl Display) -> Result<T>;
    fn with_context<F>(self, f: F) -> Result<T>
       where F: FnOnce() -> String;
}
Expand description

Trait for adding context to errors.

This trait allows chaining contextual information to errors, making it easier to track the source and cause of errors.

§Example

use rh_foundation::ErrorContext;

fn read_config() -> rh_foundation::Result<String> {
    std::fs::read_to_string("config.json")
        .context("Failed to read configuration file")?;
    Ok("config".to_string())
}

Required Methods§

Source

fn context(self, context: impl Display) -> Result<T>

Add context to an error

Source

fn with_context<F>(self, f: F) -> Result<T>
where F: FnOnce() -> String,

Add context with a function (lazy evaluation)

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.

Implementations on Foreign Types§

Source§

impl<T, E> ErrorContext<T, E> for Result<T, E>
where E: Error + Send + Sync + 'static,

Source§

fn context(self, context: impl Display) -> Result<T>

Source§

fn with_context<F>(self, f: F) -> Result<T>
where F: FnOnce() -> String,

Implementors§