#error

error-chain-mini

error-chain for minimalist

8 releases

Uses old Rust 2015

0.2.0 May 25, 2018
0.1.2 May 16, 2018
0.0.4 May 14, 2018
0.0.2 Apr 21, 2018

#742 in Rust patterns

Download history 8/week @ 2022-01-18 3/week @ 2022-01-25 2/week @ 2022-02-01 3/week @ 2022-02-08 2/week @ 2022-02-15 10/week @ 2022-02-22 3/week @ 2022-03-01 3/week @ 2022-03-08 30/week @ 2022-03-15 25/week @ 2022-03-22 3/week @ 2022-03-29 3/week @ 2022-04-05 6/week @ 2022-04-12 3/week @ 2022-04-19 45/week @ 2022-04-26 35/week @ 2022-05-03

90 downloads per month
Used in telegrambot

MIT/Apache

26KB
262 lines

error-chain-mini

crates.io documentation Build Status

I think error-chain is good, especially I love chain_err method.

However, sometimes I feel it too complex. I don't want to generate ResultExt and ChainedError by macro. Isn't it confusing?

So, I made this tiny library, providing very straight forward implementation of ResultExt, ChainedError, and some related traits.

In addition, You can use derive to implement your own ErrorKind type.

Example

extern crate error_chain_mini;
#[macro_use]
extern crate error_chain_mini_derive;
use std::io;
use error_chain_mini::*;
use std::error::Error;
#[derive(ErrorKind)]
enum MyErrorKind {
    #[msg(short = "io error", detailed = "inner: {:?}", _0)]
    IoError(io::Error),
    #[msg(short = "index error", detailed = "invalid index: {:?}", _0)]
    IndexEroor(usize),
    TrivialError,
}
type MyError = ChainedError<MyErrorKind>;
type MyResult<T> = Result<T, MyError>;
fn always_fail() -> MyResult<()> {
    Err(MyErrorKind::TrivialError.into_with("Oh my god!"))
}
fn main() {
    assert_eq!("index error invalid index: 10", MyErrorKind::IndexEroor(10).full());
    let chained = always_fail().chain_err("Error in main()");
    assert!(chained.is_err());
    if let Err(chained) = chained {
        assert_eq!(chained.description(), "MyErrorKind::TrivialError");
        assert_eq!(chained.context[0], "Oh my god!");
        assert_eq!(chained.context[1], "Error in main()");
    }
}

Required minimum version of Rust

1.26.0 (match_default_bindings is needed)

License

This project is licensed under either of

at your option.

No runtime deps