libimagentrycategory: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 15:55:20 +02:00
parent 262aae39f4
commit 8d8a91e7c5
4 changed files with 36 additions and 27 deletions

View File

@ -26,7 +26,7 @@ use libimagstore::store::Entry;
use libimagerror::into::IntoError;
use error::CategoryErrorKind as CEK;
use error::MapErrInto;
use error::ResultExt;
use result::Result;
use register::CategoryRegister;
@ -64,7 +64,7 @@ impl EntryCategory for Entry {
fn set_category(&mut self, s: Category) -> Result<()> {
self.get_header_mut()
.insert(&String::from("category.value"), Value::String(s.into()))
.map_err_into(CEK::HeaderWriteError)
.chain_err(|| CEK::HeaderWriteError)
.map(|_| ())
}
@ -86,17 +86,17 @@ impl EntryCategory for Entry {
&TQEK::IdentifierNotFoundInDocument(_) => Ok(None),
_ => Err(res),
}
.map_err_into(CEK::HeaderReadError),
.chain_err(|| CEK::HeaderReadError),
Ok(Some(&Value::String(ref s))) => Ok(Some(s.clone().into())),
Ok(None) => Err(CEK::StoreReadError.into_error()).map_err_into(CEK::HeaderReadError),
Ok(_) => Err(CEK::TypeError.into_error()).map_err_into(CEK::HeaderReadError),
Ok(None) => Err(CEK::StoreReadError.into_error()).chain_err(|| CEK::HeaderReadError),
Ok(_) => Err(CEK::TypeError.into_error()).chain_err(|| CEK::HeaderReadError),
}
}
fn has_category(&self) -> Result<bool> {
self.get_header().read(&String::from("category.value"))
.map_err_into(CEK::HeaderReadError)
.chain_err(|| CEK::HeaderReadError)
.map(|e| e.is_some())
}

View File

@ -17,6 +17,10 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::error::Error;
use libimagerror::into::IntoError;
error_chain! {
types {
CategoryError, CategoryErrorKind, ResultExt, Result;
@ -43,17 +47,23 @@ error_chain! {
display("Header read error")
}
HeaderWriteError {
description("Header write error")
display("Header write error")
}
CategoryDoesNotExist {
description("Category does not exist")
display("Category does not exist")
}
TypeError {
description("Type Error")
display("Type Error")
}
}
}
pub use self::error::CategoryError;
pub use self::error::CategoryErrorKind;
pub use self::error::MapErrInto;
impl IntoError for CategoryErrorKind {
type Target = CategoryError;
@ -61,7 +71,7 @@ impl IntoError for CategoryErrorKind {
CategoryError::from_kind(self)
}
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
CategoryError::from_kind(self)
}
}

View File

@ -43,7 +43,6 @@ extern crate is_match;
extern crate log;
#[macro_use] extern crate error_chain;
#[macro_use]
extern crate libimagerror;
#[macro_use]
extern crate libimagstore;

View File

@ -31,7 +31,7 @@ use libimagerror::into::IntoError;
use category::Category;
use error::CategoryErrorKind as CEK;
use error::MapErrInto;
use error::ResultExt;
use result::Result;
pub const CATEGORY_REGISTER_NAME_FIELD_PATH : &'static str = "category.register.name";
@ -81,13 +81,13 @@ impl CategoryRegister for Store {
warn!("Setting category header replaced existing value: {:?}", opt);
})
.map(|_| true)
.map_err_into(CEK::HeaderWriteError)
.map_err_into(CEK::StoreWriteError)
.chain_err(|| CEK::HeaderWriteError)
.chain_err(|| CEK::StoreWriteError)
}
Err(store_error) => if is_match!(store_error.err_type(), SEK::EntryAlreadyExists) {
Err(store_error) => if is_match!(store_error.kind(), &SEK::EntryAlreadyExists) {
Ok(false)
} else {
Err(store_error).map_err_into(CEK::StoreWriteError)
Err(store_error).chain_err(|| CEK::StoreWriteError)
}
}
}
@ -96,13 +96,13 @@ impl CategoryRegister for Store {
fn delete_category(&self, name: &str) -> Result<()> {
let sid = try!(mk_category_storeid(self.path().clone(), name));
self.delete(sid).map_err_into(CEK::StoreWriteError)
self.delete(sid).chain_err(|| CEK::StoreWriteError)
}
/// Get all category names
fn all_category_names(&self) -> Result<CategoryNameIter> {
self.retrieve_for_module("category")
.map_err_into(CEK::StoreReadError)
.chain_err(|| CEK::StoreReadError)
.map(|iter| CategoryNameIter::new(self, iter))
}
@ -114,7 +114,7 @@ impl CategoryRegister for Store {
let sid = try!(mk_category_storeid(self.path().clone(), name));
self.get(sid)
.map_err_into(CEK::StoreWriteError)
.chain_err(|| CEK::StoreWriteError)
}
}
@ -212,26 +212,26 @@ fn mk_category_storeid(base: PathBuf, s: &str) -> Result<StoreId> {
::module_path::ModuleEntryPath::new(s)
.into_storeid()
.map(|id| id.with_base(base))
.map_err_into(CEK::StoreIdHandlingError)
.chain_err(|| CEK::StoreIdHandlingError)
}
#[inline]
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
sid.exists()
.map_err_into(CEK::StoreIdHandlingError)
.chain_err(|| CEK::StoreIdHandlingError)
.and_then(|bl| {
if bl {
store.get(sid)
.map_err_into(CEK::StoreReadError)
.chain_err(|| CEK::StoreReadError)
.and_then(|fle| {
if let Some(fle) = fle {
match fle.get_header()
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
.map_err_into(CEK::HeaderReadError)
.chain_err(|| CEK::HeaderReadError)
{
Ok(Some(&Value::String(ref s))) => Ok(s == name),
Ok(_) => Err(CEK::TypeError.into_error()),
Err(e) => Err(e).map_err_into(CEK::HeaderReadError),
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
}
} else {
Ok(false)
@ -277,12 +277,12 @@ impl<'a> Iterator for CategoryNameIter<'a> {
.map(|sid| {
self.0
.get(sid)
.map_err_into(CEK::StoreReadError)
.chain_err(|| CEK::StoreReadError)
.and_then(|fle| fle.ok_or(CEK::StoreReadError.into_error()))
.and_then(|fle| match fle.get_header().read(&query) {
Ok(Some(&Value::String(ref s))) => Ok(Category::from(s.clone())),
Ok(_) => Err(CEK::TypeError.into_error()),
Err(e) => Err(e).map_err_into(CEK::HeaderReadError),
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
})
})
}