error-chain 0.4.2

Yet another error boilerplate library
Documentation

Build Status Latest Version

error-chain - Consistent error handling for Rust

error-chain is a crate for dealing with Rust error boilerplate. It provides a few unique features:

  • No error is ever discarded. This library primarily makes it easy to "chain" errors with the chain_err method.
  • Introducing new errors is trivial. Simple errors can be introduced at the error site with just a string.
  • Errors create and propagate backtraces.

Documentation.

Quick start

Add this to Cargo.toml, under [dependencies]:

error-chain = "0.4"

Write this at the top of your crate:

#![recursion_limit = "1024"];

Again near the top of your crate, import the error_chain crate and its macros:

#[macro_use]
extern crate error_chain;

Add an errors module to your crate:

mod errors;

Add a file for that module called errors.rs and put this inside:

error_chain! { }

That's the setup. Now when writing modules for your crate, import everything from the errors module:

use errors::*;

Create functions that return Result, which is defined by the error_chain! macro, and start chaining errors!

fn do_error_prone_work() -> Result<()> {
    let file = try!(File::open("foo").chain_err(|| "couldn't open file"));
    try!(file.write_str("important").chain_err(|| "couldn't write file"));

    Ok(())
}

License

MIT/Apache-2.0