Created
May 12, 2017 21:39
-
-
Save richinseattle/5424a0dd8780a656ce770b3b0e9d1577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rust main skeleton | |
// | |
// references: | |
// error_chain: https://brson.github.io/2016/11/30/starting-with-error-chain | |
#![recursion_limit = "1024"] | |
#[macro_use] | |
extern crate error_chain; | |
// Create the Error, ErrorKind, ResultExt, and Result types | |
// note: other modules `use errors::*;` to get access to everything | |
mod errors { error_chain! { } } | |
use errors::*; | |
fn main() { | |
if let Err(ref e) = run() { | |
println!("error: {}", e); | |
for e in e.iter().skip(1) { | |
println!("caused by: {}", e); | |
} | |
// note: run with `RUST_BACKTRACE=1` if no backtrace is available | |
if let Some(backtrace) = e.backtrace() { | |
println!("backtrace: {:?}", backtrace); | |
} | |
::std::process::exit(1); | |
} | |
} | |
// Most functions will return the `Result` type, imported from the | |
// `errors` module. It is a typedef of the standard `Result` type | |
// for which the error type is always our own `Error`. | |
fn run() -> Result<()> { | |
// example | |
use std::fs::File; | |
// This operation will fail | |
File::open("contacts").chain_err(|| "unable to open contacts file")?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment