libimagentryannotation: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 15:50:54 +02:00
parent d443b83b52
commit 262aae39f4
4 changed files with 17 additions and 17 deletions

View file

@ -32,7 +32,7 @@ use toml_query::insert::TomlValueInsertExt;
use result::Result; use result::Result;
use error::AnnotationErrorKind as AEK; use error::AnnotationErrorKind as AEK;
use error::MapErrInto; use error::ResultExt;
pub trait Annotateable { pub trait Annotateable {
@ -51,16 +51,16 @@ impl Annotateable for Entry {
fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> { fn annotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<FileLockEntry<'a>> {
store.retrieve(PathBuf::from(ann_name)) store.retrieve(PathBuf::from(ann_name))
.map_err_into(AEK::StoreWriteError) .chain_err(|| AEK::StoreWriteError)
.and_then(|mut anno| { .and_then(|mut anno| {
anno.get_header_mut() anno.get_header_mut()
.insert("annotation.is_annotation", Value::Boolean(true)) .insert("annotation.is_annotation", Value::Boolean(true))
.map_err_into(AEK::HeaderWriteError) .chain_err(|| AEK::HeaderWriteError)
.map(|_| anno) .map(|_| anno)
}) })
.and_then(|mut anno| { .and_then(|mut anno| {
anno.add_internal_link(self) anno.add_internal_link(self)
.map_err_into(AEK::LinkingError) .chain_err(|| AEK::LinkingError)
.map(|_| anno) .map(|_| anno)
}) })
} }
@ -68,7 +68,7 @@ impl Annotateable for Entry {
fn is_annotation(&self) -> Result<bool> { fn is_annotation(&self) -> Result<bool> {
self.get_header() self.get_header()
.read("annotation.is_annotation") .read("annotation.is_annotation")
.map_err_into(AEK::StoreReadError) .chain_err(|| AEK::StoreReadError)
.and_then(|res| match res { .and_then(|res| match res {
Some(&Value::Boolean(b)) => Ok(b), Some(&Value::Boolean(b)) => Ok(b),
None => Ok(false), None => Ok(false),

View file

@ -26,7 +26,7 @@ use libimagstore::storeid::StoreIdIterator;
use result::Result; use result::Result;
use error::AnnotationErrorKind as AEK; use error::AnnotationErrorKind as AEK;
use error::MapErrInto; use error::ResultExt;
use self::iter::*; use self::iter::*;
@ -45,7 +45,7 @@ impl<'a> AnnotationFetcher<'a> for Store {
fn all_annotations(&'a self) -> Result<AnnotationIter<'a>> { fn all_annotations(&'a self) -> Result<AnnotationIter<'a>> {
Note::all_notes(self) Note::all_notes(self)
.map(|iter| AnnotationIter::new(iter)) .map(|iter| AnnotationIter::new(iter))
.map_err_into(AEK::StoreReadError) .chain_err(|| AEK::StoreReadError)
} }
/// Get all annotations (in an iterator) for an entry /// Get all annotations (in an iterator) for an entry
@ -57,7 +57,7 @@ impl<'a> AnnotationFetcher<'a> for Store {
/// entry, but should normally be not that heavy. /// entry, but should normally be not that heavy.
fn annotations_for_entry(&'a self, entry: &Entry) -> Result<AnnotationIter<'a>> { fn annotations_for_entry(&'a self, entry: &Entry) -> Result<AnnotationIter<'a>> {
entry.get_internal_links() entry.get_internal_links()
.map_err_into(AEK::StoreReadError) .chain_err(|| AEK::StoreReadError)
.map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone())))) .map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()))))
.map(|iter| NoteIterator::new(self, iter)) .map(|iter| NoteIterator::new(self, iter))
.map(|iter| AnnotationIter::new(iter)) .map(|iter| AnnotationIter::new(iter))
@ -76,7 +76,7 @@ pub mod iter {
use result::Result; use result::Result;
use error::AnnotationErrorKind as AEK; use error::AnnotationErrorKind as AEK;
use error::MapErrInto; use error::ResultExt;
#[derive(Debug)] #[derive(Debug)]
pub struct AnnotationIter<'a>(NoteIterator<'a>); pub struct AnnotationIter<'a>(NoteIterator<'a>);
@ -100,10 +100,10 @@ pub mod iter {
Ok(None) => continue, // not an annotation Ok(None) => continue, // not an annotation
Ok(Some(&Value::Boolean(true))) => return Some(Ok(note)), Ok(Some(&Value::Boolean(true))) => return Some(Ok(note)),
Ok(Some(_)) => return Some(Err(AEK::HeaderTypeError.into_error())), Ok(Some(_)) => return Some(Err(AEK::HeaderTypeError.into_error())),
Err(e) => return Some(Err(e).map_err_into(AEK::HeaderReadError)), Err(e) => return Some(Err(e).chain_err(|| AEK::HeaderReadError)),
} }
}, },
Some(Err(e)) => return Some(Err(e).map_err_into(AEK::StoreReadError)), Some(Err(e)) => return Some(Err(e).chain_err(|| AEK::StoreReadError)),
None => return None, // iterator consumed None => return None, // iterator consumed
} }
} }

View file

@ -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 {
AnnotationError, AnnotationErrorKind, ResultExt, Result; AnnotationError, AnnotationErrorKind, ResultExt, Result;
@ -56,10 +60,6 @@ error_chain! {
} }
} }
pub use self::error::AnnotationError;
pub use self::error::AnnotationErrorKind;
pub use self::error::MapErrInto;
impl IntoError for AnnotationErrorKind { impl IntoError for AnnotationErrorKind {
type Target = AnnotationError; type Target = AnnotationError;
@ -67,7 +67,7 @@ impl IntoError for AnnotationErrorKind {
AnnotationError::from_kind(self) AnnotationError::from_kind(self)
} }
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target { fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
AnnotationError::from_kind(self) AnnotationError::from_kind(self)
} }
} }

View file

@ -39,7 +39,7 @@ extern crate toml;
extern crate toml_query; extern crate toml_query;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
#[macro_use] extern crate libimagerror; extern crate libimagerror;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagentrylink; extern crate libimagentrylink;
extern crate libimagnotes; extern crate libimagnotes;