From 4e2937fc614538fd73363a41667c9472fb6ff5f3 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Tue, 27 Dec 2016 20:58:51 +0100 Subject: refactor error handling with error_chain --- src/bin/mt-tool.rs | 36 ++++++++++++++++++++++++++++-------- src/bin/mt-webface.rs | 5 +++++ 2 files changed, 33 insertions(+), 8 deletions(-) (limited to 'src/bin') diff --git a/src/bin/mt-tool.rs b/src/bin/mt-tool.rs index c04d5ab..e554636 100644 --- a/src/bin/mt-tool.rs +++ b/src/bin/mt-tool.rs @@ -7,6 +7,7 @@ extern crate modelica_parser; use modelthing::transpile_scheme::TranspileScheme; use modelthing::transpile_js::TranspileJS; use modelthing::modelica_model::ModelicaModelExt; +use modelthing::Result; use clap::{App, SubCommand}; use std::io::Read; use std::fs::File; @@ -35,7 +36,7 @@ fn parse_modelica_files(paths: Vec) { } } -fn main() { +fn run() -> Result<()> { let matches = App::new("mt-tool") .version(env!("CARGO_PKG_VERSION")) @@ -69,18 +70,18 @@ fn main() { let dir = Path::new(subm.value_of("DIR").unwrap()); let indep = subm.values_of_lossy("indep").unwrap(); let dep = subm.values_of_lossy("dep").unwrap(); - let me = modelthing::load_model_entry(dir).unwrap(); - println!("{:?}", me.ast.solve_for(indep, dep)); + let me = try!(modelthing::load_model_entry(dir)); + println!("{:?}", try!(me.ast.solve_for(indep, dep))); }, ("transpile_scheme", Some(subm)) => { let dir = Path::new(subm.value_of("DIR").unwrap()); - let me = modelthing::load_model_entry(dir).unwrap(); - println!("{}", me.ast.repr_scheme().unwrap()); + let me = try!(modelthing::load_model_entry(dir)); + println!("{}", try!(me.ast.repr_scheme())); }, ("transpile_js", Some(subm)) => { let dir = Path::new(subm.value_of("DIR").unwrap()); - let me = modelthing::load_model_entry(dir).unwrap(); - println!("{}", me.ast.repr_js().unwrap()); + let me = try!(modelthing::load_model_entry(dir)); + println!("{}", try!(me.ast.repr_js())); }, ("list", Some(subm)) => { let dir = Path::new(subm.value_of("DIR").unwrap_or("examples")); @@ -90,7 +91,7 @@ fn main() { }, ("load", Some(subm)) => { let dir = Path::new(subm.value_of("DIR").unwrap()); - let me = modelthing::load_model_entry(dir).unwrap(); + let me = try!(modelthing::load_model_entry(dir)); println!("{:?}", me); }, _ => { @@ -99,4 +100,23 @@ fn main() { ::std::process::exit(-1); }, } + Ok(()) +} + +fn main() { + if let Err(ref e) = run() { + println!("error: {}", e); + + for e in e.iter().skip(1) { + println!("caused by: {}", e); + } + + // The backtrace is not always generated. Try to run this example + // with `RUST_BACKTRACE=1`. + if let Some(backtrace) = e.backtrace() { + println!("backtrace: {:?}", backtrace); + } + + ::std::process::exit(1); + } } diff --git a/src/bin/mt-webface.rs b/src/bin/mt-webface.rs index b35deec..4cdc509 100644 --- a/src/bin/mt-webface.rs +++ b/src/bin/mt-webface.rs @@ -15,6 +15,11 @@ use getopts::Options; use pencil::Pencil; use pencil::{Request, PencilResult, Response, HTTPError, PencilError}; +/* +This command doesn't use error_chain (or raise errors in general) because the +web framework handles panics as 500 errors automatically. +*/ + fn home(r: &mut Request) -> PencilResult { let context: BTreeMap = BTreeMap::new(); -- cgit v1.2.3