Trait error_chain_mini::ResultExt[][src]

pub trait ResultExt {
    type OkType;
    type ErrType;
    fn chain_err<C, K, F>(self, op: F) -> Result<Self::OkType, ChainedError<K>>
    where
        F: FnOnce() -> C,
        K: ErrorKind,
        C: ErrorContext,
        Self::ErrType: Into<ChainedError<K>>
;
fn into_chained<C, K, F>(
        self,
        op: F
    ) -> Result<Self::OkType, ChainedError<K>>
    where
        F: FnOnce() -> C,
        K: ErrorKind + From<Self::ErrType>,
        C: ErrorContext
;
fn convert<K, U>(self) -> Result<Self::OkType, ChainedError<U>>
    where
        K: ErrorKind,
        Self::ErrType: Into<ChainedError<K>>,
        U: From<K> + ErrorKind
;
fn convert_with<K, U, F>(
        self,
        converter: F
    ) -> Result<Self::OkType, ChainedError<U>>
    where
        K: ErrorKind,
        Self::ErrType: Into<ChainedError<K>>,
        U: ErrorKind,
        F: FnOnce(K) -> U
; }

Result extension to integrate with ChainedError

Associated Types

Required Methods

Almost same as chain_err, but takes closure. If you want to do expensive operation like format! to generate error message, use this method instead of chain_err.

Usage

use error_chain_mini::*;
#[derive(ErrorKind, Eq, PartialEq, Debug)]
#[msg(short = "My Error")]
struct MyError;
fn my_func() -> Result<(), ChainedError<MyError>>{
    let chained = MyError{}.into_with(|| "Error in my_func");
    Err(chained)
}
let chained = my_func().chain_err(|| "Chained");
assert!(chained.is_err());
if let Err(e) = chained {
    let msg = format!("{}", e);
    assert_eq!(msg, r#"
kind: My Error
context  0: Error in my_func
context  1: Chained
"#);
}

Takes Result and context then convert its error type into ChainedError with context.

Usage

use error_chain_mini::*;
use std::io;
use std::fs::File;
#[derive(Debug, ErrorKind)]
enum MyError {
    #[msg(short = "io error", detailed = "{:?}", _0)]
    Io(io::Error),
    Misc
}
impl From<io::Error> for MyError {
    fn from(e: io::Error) -> Self {
        MyError::Io(e)
    }
}
let file: Result<_, ChainedError<MyError>> = File::open("not_existing_file").into_chained(|| "In io()");

Convert Result<Ok, ChainedError<T>> into Result<Ok, ChainedError<U>> using std::convert::From.

Usage

mod external {
    #[derive(ErrorKind, Eq, PartialEq, Debug)]
    #[msg(short = "error in external")]
    pub struct ExtErrorKind;
    pub type Error = ChainedError<ExtErrorKind>;
    pub fn func() -> Result<(), Error> {
        Err(ExtErrorKind{}.into_with(|| "In external::func()"))
    }
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
    Internal,
    #[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
    External(ExtErrorKind),
};
impl From<ExtErrorKind> for MyErrorKind {
    fn from(e: ExtErrorKind) -> MyErrorKind {
        MyErrorKind::External(e)
    }
}
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error> = external::func().convert();
if let Err(chained) = chained {
    assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
}

Convert Result<Ok, ChainedError<T>> into Result<Ok, ChainedError<U>> using closure.

Usage

mod external {
    #[derive(ErrorKind, Eq, PartialEq, Debug)]
    #[msg(short = "error in external")]
    pub struct ExtErrorKind;
    pub type Error = ChainedError<ExtErrorKind>;
    pub fn func() -> Result<(), Error> {
        Err(ExtErrorKind{}.into_with(|| "In external::func()"))
    }
}
use external::{self, ExtErrorKind};
#[derive(ErrorKind, Eq, PartialEq, Debug)]
enum MyErrorKind {
    Internal,
    #[msg(short = "from mod 'external'", detailed = "{:?}", _0)]
    External(ExtErrorKind),
};
type Error = ChainedError<MyErrorKind>;
let chained: Result<(), Error>
    = external::func().convert_with(|e| MyErrorKind::External(e));
if let Err(chained) = chained {
    assert_eq!(*chained.kind(), MyErrorKind::External(ExtErrorKind {}));
}

Implementations on Foreign Types

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

Implementors