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 |
#1745 in Rust patterns
Used in telegrambot
27KB
262 lines
error-chain-mini
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
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.