From 677a5e88864698125264f635982a5b19351fe918 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 3 Sep 2017 16:13:08 +0200 Subject: [PATCH] libimagdiary: Rewrite error handling --- lib/domain/libimagdiary/src/diary.rs | 10 +++++----- lib/domain/libimagdiary/src/diaryid.rs | 10 +++++----- lib/domain/libimagdiary/src/error.rs | 10 +++++----- lib/domain/libimagdiary/src/iter.rs | 4 ++-- lib/domain/libimagdiary/src/lib.rs | 2 +- lib/domain/libimagdiary/src/viewer.rs | 6 +++--- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index 431b6320..6617f392 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -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> { diff --git a/lib/domain/libimagdiary/src/diaryid.rs b/lib/domain/libimagdiary/src/diaryid.rs index da1afc42..a08f51c0 100644 --- a/lib/domain/libimagdiary/src/diaryid.rs +++ b/lib/domain/libimagdiary/src/diaryid.rs @@ -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 = next_component(&mut cmps) .and_then(|s| s.parse::() - .map_err_into(DEK::IdParseError)); + .chain_err(|| DEK::IdParseError)); let month: Result = next_component(&mut cmps) .and_then(|s| s.parse::() - .map_err_into(DEK::IdParseError)); + .chain_err(|| DEK::IdParseError)); let year: Result = next_component(&mut cmps) .and_then(|s| s.parse::() - .map_err_into(DEK::IdParseError)); + .chain_err(|| DEK::IdParseError)); let name = next_component(&mut cmps).map(String::from); diff --git a/lib/domain/libimagdiary/src/error.rs b/lib/domain/libimagdiary/src/error.rs index 5eb12274..fbea9795 100644 --- a/lib/domain/libimagdiary/src/error.rs +++ b/lib/domain/libimagdiary/src/error.rs @@ -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) -> Self::Target { + fn into_error_with_cause(self, _: Box) -> Self::Target { DiaryError::from_kind(self) } } diff --git a/lib/domain/libimagdiary/src/iter.rs b/lib/domain/libimagdiary/src/iter.rs index de849872..285dc34e 100644 --- a/lib/domain/libimagdiary/src/iter.rs +++ b/lib/domain/libimagdiary/src/iter.rs @@ -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 { diff --git a/lib/domain/libimagdiary/src/lib.rs b/lib/domain/libimagdiary/src/lib.rs index 0ef328c5..90c14679 100644 --- a/lib/domain/libimagdiary/src/lib.rs +++ b/lib/domain/libimagdiary/src/lib.rs @@ -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; diff --git a/lib/domain/libimagdiary/src/viewer.rs b/lib/domain/libimagdiary/src/viewer.rs index 93b155a7..9efd293f 100644 --- a/lib/domain/libimagdiary/src/viewer.rs +++ b/lib/domain/libimagdiary/src/viewer.rs @@ -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"); }