libimagrt: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 14:38:46 +02:00
parent 2df99524e7
commit b6909a2c86
4 changed files with 34 additions and 29 deletions

View file

@ -18,12 +18,13 @@
// //
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result as RResult;
use std::ops::Deref; use std::ops::Deref;
use toml::Value; use toml::Value;
use clap::App; use clap::App;
use std::error::Error;
error_chain! { error_chain! {
types { types {
ConfigError, ConfigErrorKind, ResultExt, Result; ConfigError, ConfigErrorKind, ResultExt, Result;
@ -57,7 +58,6 @@ error_chain! {
} }
} }
pub use self::error::{ConfigError, ConfigErrorKind, MapErrInto};
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
impl IntoError for ConfigErrorKind { impl IntoError for ConfigErrorKind {
@ -67,7 +67,7 @@ impl IntoError for ConfigErrorKind {
ConfigError::from_kind(self) ConfigError::from_kind(self)
} }
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target { fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
ConfigError::from_kind(self) ConfigError::from_kind(self)
} }
} }
@ -160,8 +160,7 @@ impl Configuration {
pub fn override_config(&mut self, v: Vec<String>) -> Result<()> { pub fn override_config(&mut self, v: Vec<String>) -> Result<()> {
use libimagutil::key_value_split::*; use libimagutil::key_value_split::*;
use libimagutil::iter::*; use libimagutil::iter::*;
use self::error::ConfigErrorKind as CEK; use self::ConfigErrorKind as CEK;
use self::error::MapErrInto;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
@ -178,7 +177,7 @@ impl Configuration {
.map(|(k, v)| self .map(|(k, v)| self
.config .config
.read(&k[..]) .read(&k[..])
.map_err_into(CEK::TOMLParserError) .chain_err(|| CEK::TOMLParserError)
.map(|toml| match toml { .map(|toml| match toml {
Some(value) => match into_value(value, v) { Some(value) => match into_value(value, v) {
Some(v) => { Some(v) => {

View file

@ -17,20 +17,27 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
generate_error_imports!(); error_chain! {
use std::io::Error as IOError; types {
RuntimeError, RuntimeErrorKind, ResultExt, Result;
}
generate_error_types!(RuntimeError, RuntimeErrorKind, errors {
Instantiate => "Could not instantiate", Instantiate {
IOError => "IO Error", description("Could not instantiate")
ProcessExitFailure => "Process exited with failure" display("Could not instantiate")
); }
impl From<IOError> for RuntimeError { IOError {
description("IO Error")
display("IO Error")
}
fn from(ioe: IOError) -> RuntimeError { ProcessExitFailure {
RuntimeErrorKind::IOError.into_error_with_cause(Box::new(ioe)) description("Process exited with failure")
display("Process exited with failure")
} }
} }
}

View file

@ -49,7 +49,7 @@ extern crate toml_query;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
#[macro_use] extern crate libimagerror; extern crate libimagerror;
pub mod error; pub mod error;
pub mod configuration; pub mod configuration;

View file

@ -61,9 +61,8 @@ impl<'a> Runtime<'a> {
where C: Clone + CliSpec<'a> + InternalConfiguration where C: Clone + CliSpec<'a> + InternalConfiguration
{ {
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::into::IntoError;
use configuration::error::ConfigErrorKind; use configuration::ConfigErrorKind;
let matches = cli_app.clone().matches(); let matches = cli_app.clone().matches();
@ -75,8 +74,8 @@ impl<'a> Runtime<'a> {
debug!("Config path = {:?}", configpath); debug!("Config path = {:?}", configpath);
let config = match Configuration::new(&configpath) { let config = match Configuration::new(&configpath) {
Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound { Err(e) => if !is_match!(e.kind(), &ConfigErrorKind::NoConfigFileFound) {
return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e))); return Err(e).chain_err(|| RuntimeErrorKind::Instantiate);
} else { } else {
warn!("No config file found."); warn!("No config file found.");
warn!("Continuing without configuration file"); warn!("Continuing without configuration file");
@ -172,7 +171,7 @@ impl<'a> Runtime<'a> {
store: store, store: store,
} }
}) })
.map_err_into(RuntimeErrorKind::Instantiate) .chain_err(|| RuntimeErrorKind::Instantiate)
} }
/// ///
@ -388,11 +387,11 @@ impl<'a> Runtime<'a> {
let mapper = JsonMapper::new(); let mapper = JsonMapper::new();
StdIoFileAbstraction::new(&mut input, output, mapper) StdIoFileAbstraction::new(&mut input, output, mapper)
.map_err_into(RuntimeErrorKind::Instantiate) .chain_err(|| RuntimeErrorKind::Instantiate)
.and_then(|backend| { .and_then(|backend| {
self.store self.store
.reset_backend(Box::new(backend)) .reset_backend(Box::new(backend))
.map_err_into(RuntimeErrorKind::Instantiate) .chain_err(|| RuntimeErrorKind::Instantiate)
}) })
} }
@ -407,11 +406,11 @@ impl<'a> Runtime<'a> {
let mapper = JsonMapper::new(); let mapper = JsonMapper::new();
StdoutFileAbstraction::new(output, mapper) StdoutFileAbstraction::new(output, mapper)
.map_err_into(RuntimeErrorKind::Instantiate) .chain_err(|| RuntimeErrorKind::Instantiate)
.and_then(|backend| { .and_then(|backend| {
self.store self.store
.reset_backend(Box::new(backend)) .reset_backend(Box::new(backend))
.map_err_into(RuntimeErrorKind::Instantiate) .chain_err(|| RuntimeErrorKind::Instantiate)
}) })
} }