quickstart/
quickstart.rs

1// Simple and robust error handling with error-chain!
2// Use this as a template for new projects.
3
4// `error_chain!` can recurse deeply
5#![recursion_limit = "1024"]
6
7// Import the macro. Don't forget to add `error-chain` in your
8// `Cargo.toml`!
9#[macro_use]
10extern crate error_chain;
11
12// We'll put our errors in an `errors` module, and other modules in
13// this crate will `use errors::*;` to get access to everything
14// `error_chain!` creates.
15mod errors {
16    // Create the Error, ErrorKind, ResultExt, and Result types
17    error_chain! {}
18}
19
20// This only gives access within this module. Make this `pub use errors::*;`
21// instead if the types must be accessible from other modules (e.g., within
22// a `links` section).
23use errors::*;
24
25fn main() {
26    if let Err(ref e) = run() {
27        use std::io::Write;
28        let stderr = &mut ::std::io::stderr();
29        let errmsg = "Error writing to stderr";
30
31        writeln!(stderr, "error: {}", e).expect(errmsg);
32
33        for e in e.iter().skip(1) {
34            writeln!(stderr, "caused by: {}", e).expect(errmsg);
35        }
36
37        // The backtrace is not always generated. Try to run this example
38        // with `RUST_BACKTRACE=1`.
39        if let Some(backtrace) = e.backtrace() {
40            writeln!(stderr, "backtrace: {:?}", backtrace).expect(errmsg);
41        }
42
43        ::std::process::exit(1);
44    }
45}
46
47// The above main gives you maximum control over how the error is
48// formatted. If you don't care (i.e. you want to display the full
49// error during an assert) you can just call the `display_chain` method
50// on the error object
51#[allow(dead_code)]
52fn alternative_main() {
53    if let Err(ref e) = run() {
54        use error_chain::ChainedError;
55        use std::io::Write; // trait which holds `display_chain`
56        let stderr = &mut ::std::io::stderr();
57        let errmsg = "Error writing to stderr";
58
59        writeln!(stderr, "{}", e.display_chain()).expect(errmsg);
60        ::std::process::exit(1);
61    }
62}
63
64// Use this macro to auto-generate the main above. You may want to
65// set the `RUST_BACKTRACE` env variable to see a backtrace.
66// quick_main!(run);
67
68// Most functions will return the `Result` type, imported from the
69// `errors` module. It is a typedef of the standard `Result` type
70// for which the error type is always our own `Error`.
71fn run() -> Result<()> {
72    use std::fs::File;
73
74    // This operation will fail
75    File::open("tretrete").chain_err(|| "unable to open tretrete file")?;
76
77    Ok(())
78}