libimagentrycategory: Rewrite error handling
This commit is contained in:
parent
262aae39f4
commit
8d8a91e7c5
4 changed files with 36 additions and 27 deletions
|
@ -26,7 +26,7 @@ use libimagstore::store::Entry;
|
||||||
use libimagerror::into::IntoError;
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
use error::CategoryErrorKind as CEK;
|
use error::CategoryErrorKind as CEK;
|
||||||
use error::MapErrInto;
|
use error::ResultExt;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
use register::CategoryRegister;
|
use register::CategoryRegister;
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ impl EntryCategory for Entry {
|
||||||
fn set_category(&mut self, s: Category) -> Result<()> {
|
fn set_category(&mut self, s: Category) -> Result<()> {
|
||||||
self.get_header_mut()
|
self.get_header_mut()
|
||||||
.insert(&String::from("category.value"), Value::String(s.into()))
|
.insert(&String::from("category.value"), Value::String(s.into()))
|
||||||
.map_err_into(CEK::HeaderWriteError)
|
.chain_err(|| CEK::HeaderWriteError)
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,17 +86,17 @@ impl EntryCategory for Entry {
|
||||||
&TQEK::IdentifierNotFoundInDocument(_) => Ok(None),
|
&TQEK::IdentifierNotFoundInDocument(_) => Ok(None),
|
||||||
_ => Err(res),
|
_ => Err(res),
|
||||||
}
|
}
|
||||||
.map_err_into(CEK::HeaderReadError),
|
.chain_err(|| CEK::HeaderReadError),
|
||||||
|
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(Some(s.clone().into())),
|
Ok(Some(&Value::String(ref s))) => Ok(Some(s.clone().into())),
|
||||||
Ok(None) => Err(CEK::StoreReadError.into_error()).map_err_into(CEK::HeaderReadError),
|
Ok(None) => Err(CEK::StoreReadError.into_error()).chain_err(|| CEK::HeaderReadError),
|
||||||
Ok(_) => Err(CEK::TypeError.into_error()).map_err_into(CEK::HeaderReadError),
|
Ok(_) => Err(CEK::TypeError.into_error()).chain_err(|| CEK::HeaderReadError),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_category(&self) -> Result<bool> {
|
fn has_category(&self) -> Result<bool> {
|
||||||
self.get_header().read(&String::from("category.value"))
|
self.get_header().read(&String::from("category.value"))
|
||||||
.map_err_into(CEK::HeaderReadError)
|
.chain_err(|| CEK::HeaderReadError)
|
||||||
.map(|e| e.is_some())
|
.map(|e| e.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,10 @@
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
//
|
//
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use libimagerror::into::IntoError;
|
||||||
|
|
||||||
error_chain! {
|
error_chain! {
|
||||||
types {
|
types {
|
||||||
CategoryError, CategoryErrorKind, ResultExt, Result;
|
CategoryError, CategoryErrorKind, ResultExt, Result;
|
||||||
|
@ -43,16 +47,22 @@ error_chain! {
|
||||||
display("Header read error")
|
display("Header read error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HeaderWriteError {
|
||||||
|
description("Header write error")
|
||||||
|
display("Header write error")
|
||||||
|
}
|
||||||
|
|
||||||
CategoryDoesNotExist {
|
CategoryDoesNotExist {
|
||||||
description("Category does not exist")
|
description("Category does not exist")
|
||||||
display("Category does not exist")
|
display("Category does not exist")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub use self::error::CategoryError;
|
TypeError {
|
||||||
pub use self::error::CategoryErrorKind;
|
description("Type Error")
|
||||||
pub use self::error::MapErrInto;
|
display("Type Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IntoError for CategoryErrorKind {
|
impl IntoError for CategoryErrorKind {
|
||||||
type Target = CategoryError;
|
type Target = CategoryError;
|
||||||
|
@ -61,7 +71,7 @@ impl IntoError for CategoryErrorKind {
|
||||||
CategoryError::from_kind(self)
|
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)
|
CategoryError::from_kind(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,6 @@ extern crate is_match;
|
||||||
extern crate log;
|
extern crate log;
|
||||||
#[macro_use] extern crate error_chain;
|
#[macro_use] extern crate error_chain;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
|
|
|
@ -31,7 +31,7 @@ use libimagerror::into::IntoError;
|
||||||
|
|
||||||
use category::Category;
|
use category::Category;
|
||||||
use error::CategoryErrorKind as CEK;
|
use error::CategoryErrorKind as CEK;
|
||||||
use error::MapErrInto;
|
use error::ResultExt;
|
||||||
use result::Result;
|
use result::Result;
|
||||||
|
|
||||||
pub const CATEGORY_REGISTER_NAME_FIELD_PATH : &'static str = "category.register.name";
|
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);
|
warn!("Setting category header replaced existing value: {:?}", opt);
|
||||||
})
|
})
|
||||||
.map(|_| true)
|
.map(|_| true)
|
||||||
.map_err_into(CEK::HeaderWriteError)
|
.chain_err(|| CEK::HeaderWriteError)
|
||||||
.map_err_into(CEK::StoreWriteError)
|
.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)
|
Ok(false)
|
||||||
} else {
|
} 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<()> {
|
fn delete_category(&self, name: &str) -> Result<()> {
|
||||||
let sid = try!(mk_category_storeid(self.path().clone(), name));
|
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
|
/// Get all category names
|
||||||
fn all_category_names(&self) -> Result<CategoryNameIter> {
|
fn all_category_names(&self) -> Result<CategoryNameIter> {
|
||||||
self.retrieve_for_module("category")
|
self.retrieve_for_module("category")
|
||||||
.map_err_into(CEK::StoreReadError)
|
.chain_err(|| CEK::StoreReadError)
|
||||||
.map(|iter| CategoryNameIter::new(self, iter))
|
.map(|iter| CategoryNameIter::new(self, iter))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ impl CategoryRegister for Store {
|
||||||
let sid = try!(mk_category_storeid(self.path().clone(), name));
|
let sid = try!(mk_category_storeid(self.path().clone(), name));
|
||||||
|
|
||||||
self.get(sid)
|
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)
|
::module_path::ModuleEntryPath::new(s)
|
||||||
.into_storeid()
|
.into_storeid()
|
||||||
.map(|id| id.with_base(base))
|
.map(|id| id.with_base(base))
|
||||||
.map_err_into(CEK::StoreIdHandlingError)
|
.chain_err(|| CEK::StoreIdHandlingError)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
|
fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool> {
|
||||||
sid.exists()
|
sid.exists()
|
||||||
.map_err_into(CEK::StoreIdHandlingError)
|
.chain_err(|| CEK::StoreIdHandlingError)
|
||||||
.and_then(|bl| {
|
.and_then(|bl| {
|
||||||
if bl {
|
if bl {
|
||||||
store.get(sid)
|
store.get(sid)
|
||||||
.map_err_into(CEK::StoreReadError)
|
.chain_err(|| CEK::StoreReadError)
|
||||||
.and_then(|fle| {
|
.and_then(|fle| {
|
||||||
if let Some(fle) = fle {
|
if let Some(fle) = fle {
|
||||||
match fle.get_header()
|
match fle.get_header()
|
||||||
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
|
.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(Some(&Value::String(ref s))) => Ok(s == name),
|
||||||
Ok(_) => Err(CEK::TypeError.into_error()),
|
Ok(_) => Err(CEK::TypeError.into_error()),
|
||||||
Err(e) => Err(e).map_err_into(CEK::HeaderReadError),
|
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
|
@ -277,12 +277,12 @@ impl<'a> Iterator for CategoryNameIter<'a> {
|
||||||
.map(|sid| {
|
.map(|sid| {
|
||||||
self.0
|
self.0
|
||||||
.get(sid)
|
.get(sid)
|
||||||
.map_err_into(CEK::StoreReadError)
|
.chain_err(|| CEK::StoreReadError)
|
||||||
.and_then(|fle| fle.ok_or(CEK::StoreReadError.into_error()))
|
.and_then(|fle| fle.ok_or(CEK::StoreReadError.into_error()))
|
||||||
.and_then(|fle| match fle.get_header().read(&query) {
|
.and_then(|fle| match fle.get_header().read(&query) {
|
||||||
Ok(Some(&Value::String(ref s))) => Ok(Category::from(s.clone())),
|
Ok(Some(&Value::String(ref s))) => Ok(Category::from(s.clone())),
|
||||||
Ok(_) => Err(CEK::TypeError.into_error()),
|
Ok(_) => Err(CEK::TypeError.into_error()),
|
||||||
Err(e) => Err(e).map_err_into(CEK::HeaderReadError),
|
Err(e) => Err(e).chain_err(|| CEK::HeaderReadError),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue