Merge pull request #441 from matthiasbeyer/libimagstore/use-error-infrastructure

Libimagstore/use error infrastructure
This commit is contained in:
Matthias Beyer 2016-05-27 12:34:21 +02:00
commit 80aca436fe
4 changed files with 31 additions and 37 deletions

View file

@ -1,21 +1,10 @@
generate_error_imports!();
use std::convert::Into;
generate_error_types!(HookError, HookErrorKind,
generate_error_module!(
generate_error_types!(HookError, HookErrorKind,
HookExecutionError => "Hook exec error",
AccessTypeViolation => "Hook access type violation"
);
);
pub trait IntoHookError {
fn into_hookerror(self) -> HookError;
fn into_hookerror_with_cause(self, cause: Box<Error>) -> HookError;
}
impl Into<HookError> for (HookErrorKind, Box<Error>) {
fn into(self) -> HookError {
HookError::new(self.0, Some(self.1))
}
}
pub use self::error::HookError;
pub use self::error::HookErrorKind;

View file

@ -1,5 +1,6 @@
use libimagerror::into::IntoError;
use error::{StoreError, StoreErrorKind};
use error::{StoreError as SE, StoreErrorKind as SEK};
use std::io::{Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::fs::{File, OpenOptions, create_dir_all};
@ -32,21 +33,22 @@ impl LazyFile {
/**
* Get the mutable file behind a LazyFile object
*/
pub fn get_file_mut(&mut self) -> Result<&mut File, StoreError> {
pub fn get_file_mut(&mut self) -> Result<&mut File, SE> {
debug!("Getting lazy file: {:?}", self);
let file = match *self {
LazyFile::File(ref mut f) => return {
// We seek to the beginning of the file since we expect each
// access to the file to be in a different context
f.seek(SeekFrom::Start(0))
.map_err(|e| StoreError::new(StoreErrorKind::FileNotCreated, Some(Box::new(e))))
.map_err(Box::new)
.map_err(|e| SEK::FileNotCreated.into_error_with_cause(e))
.map(|_| f)
},
LazyFile::Absent(ref p) => {
try!(open_file(p).map_err(|e| {
StoreError::new(StoreErrorKind::FileNotFound,
Some(Box::new(e)))
}))
try!(open_file(p)
.map_err(Box::new)
.map_err(|e| SEK::FileNotFound.into_error_with_cause(e))
)
}
};
*self = LazyFile::File(file);
@ -59,15 +61,15 @@ impl LazyFile {
/**
* Create a file out of this LazyFile object
*/
pub fn create_file(&mut self) -> Result<&mut File, StoreError> {
pub fn create_file(&mut self) -> Result<&mut File, SE> {
debug!("Creating lazy file: {:?}", self);
let file = match *self {
LazyFile::File(ref mut f) => return Ok(f),
LazyFile::Absent(ref p) => {
try!(create_file(p).map_err(|e| {
StoreError::new(StoreErrorKind::FileNotFound,
Some(Box::new(e)))
}))
try!(create_file(p)
.map_err(Box::new)
.map_err(|e| SEK::FileNotFound.into_error_with_cause(e))
)
}
};
*self = LazyFile::File(file);

View file

@ -33,6 +33,8 @@ use hook::accessor::{ MutableHookDataAccessor,
use hook::position::HookPosition;
use hook::Hook;
use libimagerror::into::IntoError;
use self::glob_store_iter::*;
/// The Result Type returned by any interaction with the store that could fail
@ -739,7 +741,7 @@ impl EntryHeader {
let mut parser = Parser::new(s);
parser.parse()
.ok_or(ParserError::new(ParserErrorKind::TOMLParserErrors, None))
.ok_or(ParserErrorKind::TOMLParserErrors.into())
.and_then(verify_header_consistency)
.map(EntryHeader::from_table)
}
@ -1151,12 +1153,12 @@ fn build_default_header() -> Value { // BTreeMap<String, Value>
}
fn verify_header(t: &Table) -> Result<()> {
if !has_main_section(t) {
Err(SE::from(ParserError::new(ParserErrorKind::MissingMainSection, None)))
Err(SE::from(ParserErrorKind::MissingMainSection.into_error()))
} else if !has_imag_version_in_main_section(t) {
Err(SE::from(ParserError::new(ParserErrorKind::MissingVersionInfo, None)))
Err(SE::from(ParserErrorKind::MissingVersionInfo.into_error()))
} else if !has_only_tables(t) {
debug!("Could not verify that it only has tables in its base table");
Err(SE::from(ParserError::new(ParserErrorKind::NonTableInBaseTable, None)))
Err(SE::from(ParserErrorKind::NonTableInBaseTable.into_error()))
} else {
Ok(())
}
@ -1164,7 +1166,8 @@ fn verify_header(t: &Table) -> Result<()> {
fn verify_header_consistency(t: Table) -> EntryResult<Table> {
verify_header(&t)
.map_err(|e| ParserError::new(ParserErrorKind::HeaderInconsistency, Some(Box::new(e))))
.map_err(Box::new)
.map_err(|e| ParserErrorKind::HeaderInconsistency.into_error_with_cause(e))
.map(|_| t)
}

View file

@ -8,7 +8,7 @@ use std::fmt::{Debug, Formatter};
use std::fmt::Error as FmtError;
use std::result::Result as RResult;
use error::{StoreError, StoreErrorKind};
use error::StoreErrorKind as SEK;
use store::Result;
use store::Store;
@ -88,7 +88,7 @@ pub fn build_entry_path(store: &Store, path_elem: &str) -> Result<PathBuf> {
if path_elem.split('~').last().map_or(false, |v| Version::parse(v).is_err()) {
debug!("Version cannot be parsed from {:?}", path_elem);
debug!("Path does not contain version!");
return Err(StoreError::new(StoreErrorKind::StorePathLacksVersion, None));
return Err(SEK::StorePathLacksVersion.into());
}
debug!("Version checking succeeded");