error-chain
is a crate for dealing with Rust error boilerplate. It
provides a few unique features:
chain_err
method.Add this to Cargo.toml, under [dependencies]
:
toml
error-chain = "0.4"
Write this at the top of your crate:
```rust
```
Again near the top of your crate, import the error_chain
crate and its macros:
```rust
extern crate error_chain; ```
Add an errors
module to your crate:
rust
mod errors;
Add a file for that module called errors.rs
and put this inside:
rust
error_chain! { }
That's the setup. Now when writing modules for your crate,
import everything from the errors
module:
rust
use errors::*;
Create functions that return Result
, which is defined by
the error_chain!
macro, and start chaining errors!
```rust fn doerrorpronework() -> Result<()> { let file = try!(File::open("foo").chainerr(|| "couldn't open file")); try!(file.writestr("important").chainerr(|| "couldn't write file"));
Ok(())
} ```
MIT/Apache-2.0