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

View file

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

View file

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