pub trait ResultExt<T, E> {
    fn compat(self) -> Result<T, Compat<E>>;
fn context<D>(self, context: D) -> Result<T, Context<D>>
    where
        D: Display + Send + Sync + 'static
;
fn with_context<F, D>(self, f: F) -> Result<T, Context<D>>
    where
        F: FnOnce(&E) -> D,
        D: Display + Send + Sync + 'static
; }
Expand description

Extension methods for Result.

Required methods

Wraps the error in Compat to make it compatible with older error handling APIs that expect std::error::Error.

Examples
use std::error::Error;
struct CustomError;

impl Error for CustomError {
    fn description(&self) -> &str {
        "My custom error message"
    }

    fn cause(&self) -> Option<&Error> {
        None
    }
}

let x = (|| -> Result<(), failure::Error> {
    Err(CustomError).compat()?
})().with_context(|e| {
    format!("An error occured: {}", e)
}).unwrap_err();

let x = format!("{}", x);

assert_eq!(x, "An error occured: My custom error message");

Wraps the error type in a context type.

Examples
#[derive(Fail, Debug)]
#[fail(display = "")]
struct CustomError;
  
let x = (|| -> Result<(), failure::Error> {
    Err(CustomError)?
})().context(format!("An error occured")).unwrap_err();

let x = format!("{}", x);

assert_eq!(x, "An error occured");

Wraps the error type in a context type generated by looking at the error value.

Examples
#[derive(Fail, Debug)]
#[fail(display = "My custom error message")]
struct CustomError;

let x = (|| -> Result<(), failure::Error> {
    Err(CustomError)?
})().with_context(|e| {
    format!("An error occured: {}", e)
}).unwrap_err();

let x = format!("{}", x);

assert_eq!(x, "An error occured: My custom error message");

Implementations on Foreign Types

Implementors