libimagdiary: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 16:13:08 +02:00
parent 31dc0eebc2
commit 677a5e8886
6 changed files with 21 additions and 21 deletions

View file

@ -32,6 +32,7 @@ use entry::Entry;
use diaryid::DiaryId;
use error::DiaryError as DE;
use error::DiaryErrorKind as DEK;
use error::ResultExt;
use result::Result;
use iter::DiaryEntryIterator;
use is_in_diary::IsInDiary;
@ -67,7 +68,7 @@ impl<'a> Diary<'a> {
id.into_storeid()
.and_then(|id| self.store.retrieve(id))
.map(|fle| Entry::new(fle))
.map_err(|e| DE::new(DEK::StoreWriteError, Some(Box::new(e))))
.chain_err(|| DEK::StoreWriteError)
}
// Get an iterator for iterating over all entries
@ -75,18 +76,17 @@ impl<'a> Diary<'a> {
self.store
.retrieve_for_module("diary")
.map(|iter| DiaryEntryIterator::new(self.name, self.store, iter))
.map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e))))
.chain_err(|| DEK::StoreReadError)
}
pub fn delete_entry(&self, entry: Entry) -> Result<()> {
if !entry.is_in_diary(self.name) {
return Err(DE::new(DEK::EntryNotInDiary, None));
return Err(DE::from_kind(DEK::EntryNotInDiary));
}
let id = entry.get_location().clone();
drop(entry);
self.store.delete(id)
.map_err(|e| DE::new(DEK::StoreWriteError, Some(Box::new(e))))
self.store.delete(id).chain_err(|| DEK::StoreWriteError)
}
pub fn get_youngest_entry(&self) -> Option<Result<Entry>> {

View file

@ -32,7 +32,7 @@ use libimagstore::store::Result as StoreResult;
use error::DiaryError as DE;
use error::DiaryErrorKind as DEK;
use error::MapErrInto;
use error::ResultExt;
use libimagerror::into::IntoError;
use module_path::ModuleEntryPath;
@ -222,21 +222,21 @@ impl FromStoreId for DiaryId {
match (hour, minute) {
(Some(h), Some(m)) => Ok((h, m)),
_ => return Err(DE::new(DEK::IdParseError, None)),
_ => return Err(DE::from_kind(DEK::IdParseError)),
}
}));
let day: Result<u32,_> = next_component(&mut cmps)
.and_then(|s| s.parse::<u32>()
.map_err_into(DEK::IdParseError));
.chain_err(|| DEK::IdParseError));
let month: Result<u32,_> = next_component(&mut cmps)
.and_then(|s| s.parse::<u32>()
.map_err_into(DEK::IdParseError));
.chain_err(|| DEK::IdParseError));
let year: Result<i32,_> = next_component(&mut cmps)
.and_then(|s| s.parse::<i32>()
.map_err_into(DEK::IdParseError));
.chain_err(|| DEK::IdParseError));
let name = next_component(&mut cmps).map(String::from);

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

View file

@ -27,8 +27,8 @@ use diaryid::DiaryId;
use diaryid::FromStoreId;
use is_in_diary::IsInDiary;
use entry::Entry as DiaryEntry;
use error::DiaryError as DE;
use error::DiaryErrorKind as DEK;
use error::ResultExt;
use result::Result;
use libimagerror::trace::trace_error;
@ -119,7 +119,7 @@ impl<'a> Iterator for DiaryEntryIterator<'a> {
.store
.retrieve(next)
.map(|fle| DiaryEntry::new(fle))
.map_err(|e| DE::new(DEK::StoreReadError, Some(Box::new(e))))
.chain_err(|| DEK::StoreReadError)
);
}
} else {

View file

@ -43,7 +43,7 @@ extern crate itertools;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate libimagstore;
#[macro_use] extern crate libimagerror;
extern crate libimagerror;
extern crate libimagentryedit;
extern crate libimagentryview;
extern crate libimagrt;

View file

@ -21,7 +21,7 @@
use entry::Entry;
use error::DiaryErrorKind as DEK;
use error::MapErrInto;
use error::ResultExt;
use result::Result;
use libimagentryview::viewer::Viewer;
@ -52,8 +52,8 @@ impl DiaryViewer {
println!("{} :\n", id);
let _ = try!(self.0
.view_entry(&entry)
.map_err_into(DEK::ViewError)
.map_err_into(DEK::IOError));
.chain_err(|| DEK::ViewError)
.chain_err(|| DEK::IOError));
println!("\n---\n");
}