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 error::AnnotationErrorKind as AEK;
use error::MapErrInto;
use error::ResultExt;
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>> {
store.retrieve(PathBuf::from(ann_name))
.map_err_into(AEK::StoreWriteError)
.chain_err(|| AEK::StoreWriteError)
.and_then(|mut anno| {
anno.get_header_mut()
.insert("annotation.is_annotation", Value::Boolean(true))
.map_err_into(AEK::HeaderWriteError)
.chain_err(|| AEK::HeaderWriteError)
.map(|_| anno)
})
.and_then(|mut anno| {
anno.add_internal_link(self)
.map_err_into(AEK::LinkingError)
.chain_err(|| AEK::LinkingError)
.map(|_| anno)
})
}
@ -68,7 +68,7 @@ impl Annotateable for Entry {
fn is_annotation(&self) -> Result<bool> {
self.get_header()
.read("annotation.is_annotation")
.map_err_into(AEK::StoreReadError)
.chain_err(|| AEK::StoreReadError)
.and_then(|res| match res {
Some(&Value::Boolean(b)) => Ok(b),
None => Ok(false),

View file

@ -26,7 +26,7 @@ use libimagstore::storeid::StoreIdIterator;
use result::Result;
use error::AnnotationErrorKind as AEK;
use error::MapErrInto;
use error::ResultExt;
use self::iter::*;
@ -45,7 +45,7 @@ impl<'a> AnnotationFetcher<'a> for Store {
fn all_annotations(&'a self) -> Result<AnnotationIter<'a>> {
Note::all_notes(self)
.map(|iter| AnnotationIter::new(iter))
.map_err_into(AEK::StoreReadError)
.chain_err(|| AEK::StoreReadError)
}
/// 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.
fn annotations_for_entry(&'a self, entry: &Entry) -> Result<AnnotationIter<'a>> {
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| NoteIterator::new(self, iter))
.map(|iter| AnnotationIter::new(iter))
@ -76,7 +76,7 @@ pub mod iter {
use result::Result;
use error::AnnotationErrorKind as AEK;
use error::MapErrInto;
use error::ResultExt;
#[derive(Debug)]
pub struct AnnotationIter<'a>(NoteIterator<'a>);
@ -100,10 +100,10 @@ pub mod iter {
Ok(None) => continue, // not an annotation
Ok(Some(&Value::Boolean(true))) => return Some(Ok(note)),
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
}
}

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 {
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 {
type Target = AnnotationError;
@ -67,7 +67,7 @@ impl IntoError for AnnotationErrorKind {
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)
}
}

View file

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