[][src]Trait failure::ResultExt

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
; }

Extension methods for Result.

Required methods

fn compat(self) -> Result<T, Compat<E>>

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");

fn context<D>(self, context: D) -> Result<T, Context<D>> where
    D: Display + Send + Sync + 'static, 

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");

fn with_context<F, D>(self, f: F) -> Result<T, Context<D>> where
    F: FnOnce(&E) -> D,
    D: Display + Send + Sync + 'static, 

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");
Loading content...

Implementations on Foreign Types

impl<T, E> ResultExt<T, E> for Result<T, E> where
    E: Fail
[src]

impl<T> ResultExt<T, Error> for Result<T, Error>[src]

Loading content...

Implementors

Loading content...