Merge pull request #443 from matthiasbeyer/libimagrt/use-error-infrastructure
Libimagrt/use error infrastructure
This commit is contained in:
commit
7dc3c74306
4 changed files with 19 additions and 81 deletions
|
@ -4,77 +4,14 @@ use std::ops::Deref;
|
|||
|
||||
use toml::{Parser, Value};
|
||||
|
||||
/**
|
||||
* Errors which are related to configuration-file loading
|
||||
*/
|
||||
pub mod error {
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::fmt::Error as FmtError;
|
||||
|
||||
/// The kind of an error
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ConfigErrorKind {
|
||||
NoConfigFileFound,
|
||||
}
|
||||
|
||||
/// Configuration error type
|
||||
#[derive(Debug)]
|
||||
pub struct ConfigError {
|
||||
kind: ConfigErrorKind,
|
||||
cause: Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl ConfigError {
|
||||
|
||||
/// Instantiate a new `ConfigError`, optionally with cause
|
||||
pub fn new(kind: ConfigErrorKind, cause: Option<Box<Error>>) -> ConfigError {
|
||||
ConfigError {
|
||||
kind: kind,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
///get the Kind of the Error
|
||||
pub fn err_type(&self) -> ConfigErrorKind {
|
||||
self.kind.clone()
|
||||
}
|
||||
|
||||
/// Get the string, the `ConfigError` can be described with
|
||||
pub fn as_str(e: &ConfigError) -> &'static str {
|
||||
match e.err_type() {
|
||||
ConfigErrorKind::NoConfigFileFound => "No config file found",
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for ConfigError {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "{}", ConfigError::as_str(self)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Error for ConfigError {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
ConfigError::as_str(self)
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
generate_error_module!(
|
||||
generate_error_types!(ConfigError, ConfigErrorKind,
|
||||
NoConfigFileFound => "No config file found"
|
||||
);
|
||||
);
|
||||
|
||||
use self::error::{ConfigError, ConfigErrorKind};
|
||||
|
||||
|
||||
/**
|
||||
* Result type of this module. Either `T` or `ConfigError`
|
||||
*/
|
||||
|
@ -245,6 +182,6 @@ fn fetch_config(rtp: &PathBuf) -> Result<Value> {
|
|||
.filter(|loaded| loaded.is_some())
|
||||
.nth(0)
|
||||
.map(|inner| Value::Table(inner.unwrap()))
|
||||
.ok_or_else(|| ConfigError::new(ConfigErrorKind::NoConfigFileFound, None))
|
||||
.ok_or(ConfigErrorKind::NoConfigFileFound.into())
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ use error::RuntimeErrorKind;
|
|||
use libimagstore::store::FileLockEntry;
|
||||
use libimagstore::store::Entry;
|
||||
|
||||
use libimagerror::into::IntoError;
|
||||
|
||||
pub type EditResult<T> = Result<T, RuntimeError>;
|
||||
|
||||
pub trait Edit {
|
||||
|
@ -55,18 +57,19 @@ pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> EditResult<()> {
|
|||
if let Some(mut editor) = rt.editor() {
|
||||
let exit_status = editor.arg(file_path).status();
|
||||
|
||||
match exit_status.map(|s| s.success()) {
|
||||
match exit_status.map(|s| s.success()).map_err(Box::new) {
|
||||
Ok(true) => {
|
||||
file.sync_data()
|
||||
.and_then(|_| file.seek(SeekFrom::Start(0)))
|
||||
.and_then(|_| file.read_to_string(s))
|
||||
.map(|_| ())
|
||||
.map_err(|e| RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e))))
|
||||
.map_err(Box::new)
|
||||
.map_err(|e| RuntimeErrorKind::IOError.into_error_with_cause(e))
|
||||
},
|
||||
Ok(false) => Err(RuntimeError::new(RuntimeErrorKind::ProcessExitFailure, None)),
|
||||
Err(e) => Err(RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(e)))),
|
||||
Ok(false) => Err(RuntimeErrorKind::ProcessExitFailure.into()),
|
||||
Err(e) => Err(RuntimeErrorKind::IOError.into_error_with_cause(e)),
|
||||
}
|
||||
} else {
|
||||
Err(RuntimeError::new(RuntimeErrorKind::Instantiate, None))
|
||||
Err(RuntimeErrorKind::Instantiate.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ generate_error_types!(RuntimeError, RuntimeErrorKind,
|
|||
impl From<IOError> for RuntimeError {
|
||||
|
||||
fn from(ioe: IOError) -> RuntimeError {
|
||||
RuntimeError::new(RuntimeErrorKind::IOError, Some(Box::new(ioe)))
|
||||
RuntimeErrorKind::IOError.into_error_with_cause(Box::new(ioe))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,13 +37,13 @@ impl<'a> Runtime<'a> {
|
|||
*/
|
||||
pub fn new(cli_spec: App<'a, 'a>) -> Result<Runtime<'a>, RuntimeError> {
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
||||
use libimagstore::hook::position::HookPosition;
|
||||
use libimagstore::error::StoreErrorKind;
|
||||
use libimagstorestdhook::debug::DebugHook;
|
||||
use libimagerror::trace::trace_error;
|
||||
use libimagerror::trace::trace_error_dbg;
|
||||
use libimagerror::into::IntoError;
|
||||
|
||||
use configuration::error::ConfigErrorKind;
|
||||
|
||||
|
@ -72,8 +72,7 @@ impl<'a> Runtime<'a> {
|
|||
|
||||
let cfg = match Configuration::new(&rtp) {
|
||||
Err(e) => if e.err_type() != ConfigErrorKind::NoConfigFileFound {
|
||||
let cause : Option<Box<Error>> = Some(Box::new(e));
|
||||
return Err(RuntimeError::new(RuntimeErrorKind::Instantiate, cause));
|
||||
return Err(RuntimeErrorKind::Instantiate.into_error_with_cause(Box::new(e)));
|
||||
} else {
|
||||
warn!("No config file found.");
|
||||
warn!("Continuing without configuration file");
|
||||
|
@ -128,9 +127,8 @@ impl<'a> Runtime<'a> {
|
|||
store: store,
|
||||
}
|
||||
})
|
||||
.map_err(|e| {
|
||||
RuntimeError::new(RuntimeErrorKind::Instantiate, Some(Box::new(e)))
|
||||
})
|
||||
.map_err(Box::new)
|
||||
.map_err(|e| RuntimeErrorKind::Instantiate.into_error_with_cause(e))
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue