chainerror/booksrc
2020-09-01 21:34:46 +02:00
..
end.md more documentation 2018-12-21 14:06:28 +01:00
README.md lots of documentation 2018-12-21 13:50:08 +01:00
SUMMARY.md more documentation and formatting 2020-09-01 21:34:46 +02:00
tutorial1.md rename cherr() to context() 2020-09-01 21:03:57 +02:00
tutorial2.md rename cherr() to context() 2020-09-01 21:03:57 +02:00
tutorial3.md rename cherr() to context() 2020-09-01 21:03:57 +02:00
tutorial4.md rename cherr() to context() 2020-09-01 21:03:57 +02:00
tutorial5.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial6.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial7.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial8.md rename cherr() to context() 2020-09-01 21:03:57 +02:00
tutorial9.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial10.md more documentation and formatting 2020-09-01 21:34:46 +02:00
tutorial11.md more documentation and formatting 2020-09-01 21:34:46 +02:00
tutorial12.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial13.md book: use rustdoc_include 2020-03-03 15:06:40 +01:00
tutorial14.md add derive_err_kind() and more docs 2019-03-07 17:03:35 +01:00

Crate Rust Documentation Coverage Status Maintenance

chainerror

chainerror provides an error backtrace without doing a real backtrace, so even after you strip your binaries, you still have the error backtrace.

Having nested function returning errors, the output doesn't tell where the error originates from.

use std::path::PathBuf;

type BoxedError = Box<dyn std::error::Error + Send + Sync>;
fn read_config_file(path: PathBuf) -> Result<(), BoxedError> {
    // do stuff, return other errors
    let _buf = std::fs::read_to_string(&path)?;
    // do stuff, return other errors
    Ok(())
}

fn process_config_file() -> Result<(), BoxedError> {
    // do stuff, return other errors
    let _buf = read_config_file("foo.txt".into())?;
    // do stuff, return other errors
    Ok(())
}

fn main() {
    if let Err(e) = process_config_file() {
        eprintln!("Error:\n{:?}", e);
    }
}

This gives the output:

Error:
Os { code: 2, kind: NotFound, message: "No such file or directory" }

and you have no idea where it comes from.

With chainerror, you can supply a context and get a nice error backtrace:

use chainerror::prelude::v1::*;
use std::path::PathBuf;

type BoxedError = Box<dyn std::error::Error + Send + Sync>;
fn read_config_file(path: PathBuf) -> Result<(), BoxedError> {
    // do stuff, return other errors
    let _buf = std::fs::read_to_string(&path).context(format!("Reading file: {:?}", &path))?;
    // do stuff, return other errors
    Ok(())
}

fn process_config_file() -> Result<(), BoxedError> {
    // do stuff, return other errors
    let _buf = read_config_file("foo.txt".into()).context("read the config file")?;
    // do stuff, return other errors
    Ok(())
}

fn main() {
    if let Err(e) = process_config_file() {
        eprintln!("Error:\n{:?}", e);
    }
}

with the output:

Error:
examples/simple.rs:14:51: read the config file
Caused by:
examples/simple.rs:7:47: Reading file: "foo.txt"
Caused by:
Os { code: 2, kind: NotFound, message: "No such file or directory" }

chainerror uses .source() of std::error::Error along with #[track_caller] and Location to provide a nice debug error backtrace. It encapsulates all types, which have Display + Debug and can store the error cause internally.

Along with the ChainError<T> struct, chainerror comes with some useful helper macros to save a lot of typing.

chainerror has no dependencies!

Debug information is worth it!

Tutorial

Read the Tutorial

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.