From a5bb7a9becb2483839397afb8f22e15ec0a6c23e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 3 Sep 2017 16:23:45 +0200 Subject: [PATCH] libimagtimetrack: Rewrite error handling --- lib/domain/libimagtimetrack/src/error.rs | 20 ++++++++++++++----- .../libimagtimetrack/src/iter/create.rs | 6 +++--- lib/domain/libimagtimetrack/src/iter/get.rs | 4 ++-- .../libimagtimetrack/src/iter/setendtime.rs | 4 ++-- .../libimagtimetrack/src/iter/storeid.rs | 4 ++-- lib/domain/libimagtimetrack/src/lib.rs | 3 +-- .../libimagtimetrack/src/timetracking.rs | 18 ++++++++--------- .../libimagtimetrack/src/timetrackingstore.rs | 14 ++++++------- 8 files changed, 41 insertions(+), 32 deletions(-) diff --git a/lib/domain/libimagtimetrack/src/error.rs b/lib/domain/libimagtimetrack/src/error.rs index 008074a7..3eacdc22 100644 --- a/lib/domain/libimagtimetrack/src/error.rs +++ b/lib/domain/libimagtimetrack/src/error.rs @@ -17,12 +17,26 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::error::Error; + +use libimagerror::into::IntoError; + error_chain! { types { TimeTrackError, TimeTrackErrorKind, ResultExt, Result; } errors { + StoreReadError { + description("Error while writing Store") + display("Error while writing Store") + } + + StoreWriteError { + description("Error while reading Store") + display("Error while reading Store") + } + StoreIdError { description("Error while handling StoreId") display("Error while handling StoreId") @@ -52,10 +66,6 @@ error_chain! { } } -pub use self::error::TimeTrackError; -pub use self::error::TimeTrackErrorKind; -pub use self::error::MapErrInto; - impl IntoError for TimeTrackErrorKind { type Target = TimeTrackError; @@ -63,7 +73,7 @@ impl IntoError for TimeTrackErrorKind { TimeTrackError::from_kind(self) } - fn into_error_with_cause(self, cause: Box) -> Self::Target { + fn into_error_with_cause(self, _: Box) -> Self::Target { TimeTrackError::from_kind(self) } } diff --git a/lib/domain/libimagtimetrack/src/iter/create.rs b/lib/domain/libimagtimetrack/src/iter/create.rs index 0c471aa8..03047c76 100644 --- a/lib/domain/libimagtimetrack/src/iter/create.rs +++ b/lib/domain/libimagtimetrack/src/iter/create.rs @@ -24,7 +24,7 @@ use chrono::naive::NaiveDateTime as NDT; use constants::*; use error::TimeTrackError as TTE; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use iter::storeid::TagStoreIdIter; use iter::setendtime::SetEndTimeIter; @@ -61,12 +61,12 @@ impl<'a> Iterator for CreateTimeTrackIter<'a> res.and_then(|(id, starttime)| { self.store .create(id) - .map_err_into(TTEK::StoreWriteError) + .chain_err(|| TTEK::StoreWriteError) .and_then(|mut entry| { let v = Value::String(starttime.format(DATE_TIME_FORMAT).to_string()); entry.get_header_mut() .insert(DATE_TIME_START_HEADER_PATH, v) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| entry) }) }) diff --git a/lib/domain/libimagtimetrack/src/iter/get.rs b/lib/domain/libimagtimetrack/src/iter/get.rs index 62e0a85d..a7927ff2 100644 --- a/lib/domain/libimagtimetrack/src/iter/get.rs +++ b/lib/domain/libimagtimetrack/src/iter/get.rs @@ -19,7 +19,7 @@ use error::TimeTrackError as TTE; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use libimagstore::store::FileLockEntry; use libimagstore::store::Store; @@ -46,7 +46,7 @@ impl<'a> Iterator for GetTimeTrackIter<'a> { fn next(&mut self) -> Option { self.inner.next().map(|sid| { - match self.store.get(sid).map_err_into(TTEK::StoreReadError) { + match self.store.get(sid).chain_err(|| TTEK::StoreReadError) { Ok(None) => Err(TTEK::StoreReadError.into_error()), Ok(Some(s)) => Ok(s), Err(e) => Err(e) diff --git a/lib/domain/libimagtimetrack/src/iter/setendtime.rs b/lib/domain/libimagtimetrack/src/iter/setendtime.rs index 89eb382b..778877b0 100644 --- a/lib/domain/libimagtimetrack/src/iter/setendtime.rs +++ b/lib/domain/libimagtimetrack/src/iter/setendtime.rs @@ -24,7 +24,7 @@ use chrono::naive::NaiveDateTime as NDT; use constants::*; use error::TimeTrackError as TTE; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use iter::create::CreateTimeTrackIter; use libimagstore::store::FileLockEntry; @@ -55,7 +55,7 @@ impl<'a> Iterator for SetEndTimeIter<'a> { let v = Value::String(self.datetime.format(DATE_TIME_FORMAT).to_string()); fle.get_header_mut() .insert(DATE_TIME_END_HEADER_PATH, v) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| fle) }) }) diff --git a/lib/domain/libimagtimetrack/src/iter/storeid.rs b/lib/domain/libimagtimetrack/src/iter/storeid.rs index a3071908..0a68e92c 100644 --- a/lib/domain/libimagtimetrack/src/iter/storeid.rs +++ b/lib/domain/libimagtimetrack/src/iter/storeid.rs @@ -22,7 +22,7 @@ use chrono::naive::NaiveDateTime as NDT; use constants::*; use error::TimeTrackError; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use iter::tag::TagIter; use iter::create::CreateTimeTrackIter; @@ -64,7 +64,7 @@ impl Iterator for TagStoreIdIter { let id_str = format!("{}-{}", dt, tag.as_str()); ModuleEntryPath::new(id_str) .into_storeid() - .map_err_into(TTEK::StoreIdError) + .chain_err(|| TTEK::StoreIdError) .map(|id| (id, self.datetime.clone())) }) }) diff --git a/lib/domain/libimagtimetrack/src/lib.rs b/lib/domain/libimagtimetrack/src/lib.rs index 69e5a086..0a14a664 100644 --- a/lib/domain/libimagtimetrack/src/lib.rs +++ b/lib/domain/libimagtimetrack/src/lib.rs @@ -45,12 +45,11 @@ extern crate lazy_static; extern crate is_match; #[macro_use] extern crate error_chain; -#[macro_use] -extern crate libimagerror; #[macro_use] extern crate libimagstore; extern crate libimagentrydatetime; extern crate libimagentrytag; +extern crate libimagerror; mod constants; pub mod error; diff --git a/lib/domain/libimagtimetrack/src/timetracking.rs b/lib/domain/libimagtimetrack/src/timetracking.rs index ba40ac65..481d93a2 100644 --- a/lib/domain/libimagtimetrack/src/timetracking.rs +++ b/lib/domain/libimagtimetrack/src/timetracking.rs @@ -31,7 +31,7 @@ use libimagerror::into::IntoError; use tag::TimeTrackingTag as TTT; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use result::Result; use constants::*; @@ -65,7 +65,7 @@ impl TimeTracking for Entry { fn get_timetrack_tag(&self) -> Result { self.get_header() .read(DATE_TIME_TAG_HEADER_PATH) - .map_err_into(TTEK::HeaderReadError) + .chain_err(|| TTEK::HeaderReadError) .and_then(|value| match value { Some(&Value::String(ref s)) => Ok(s.clone().into()), Some(_) => Err(TTEK::HeaderFieldTypeError.into_error()), @@ -78,21 +78,21 @@ impl TimeTracking for Entry { self.get_header_mut() .insert(DATE_TIME_START_HEADER_PATH, Value::String(s)) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| ()) } fn get_start_datetime(&self) -> Result> { self.get_header() .read(DATE_TIME_START_HEADER_PATH) - .map_err_into(TTEK::HeaderReadError) + .chain_err(|| TTEK::HeaderReadError) .and_then(header_value_to_dt) } fn delete_start_datetime(&mut self) -> Result<()> { self.get_header_mut() .delete(DATE_TIME_START_HEADER_PATH) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| ()) } @@ -101,21 +101,21 @@ impl TimeTracking for Entry { self.get_header_mut() .insert(DATE_TIME_END_HEADER_PATH, Value::String(s)) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| ()) } fn get_end_datetime(&self) -> Result> { self.get_header() .read(DATE_TIME_END_HEADER_PATH) - .map_err_into(TTEK::HeaderReadError) + .chain_err(|| TTEK::HeaderReadError) .and_then(header_value_to_dt) } fn delete_end_datetime(&mut self) -> Result<()> { self.get_header_mut() .delete(DATE_TIME_END_HEADER_PATH) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| ()) } @@ -141,7 +141,7 @@ fn header_value_to_dt(val: Option<&Value>) -> Result> { match val { Some(&Value::String(ref s)) => { NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT) - .map_err_into(TTEK::DateTimeParserError) + .chain_err(|| TTEK::DateTimeParserError) .map(Some) }, diff --git a/lib/domain/libimagtimetrack/src/timetrackingstore.rs b/lib/domain/libimagtimetrack/src/timetrackingstore.rs index 94e993ac..ccd03fd5 100644 --- a/lib/domain/libimagtimetrack/src/timetrackingstore.rs +++ b/lib/domain/libimagtimetrack/src/timetrackingstore.rs @@ -33,7 +33,7 @@ use libimagentrydatetime::datepath::compiler::DatePathCompiler; use result::Result; use constants::*; use error::TimeTrackErrorKind as TTEK; -use error::MapErrInto; +use error::ResultExt; use iter::get::GetTimeTrackIter; use tag::TimeTrackingTag as TTT; @@ -71,24 +71,24 @@ impl<'a> TimeTrackStore<'a> for Store { use std::path::PathBuf; COMPILER.compile(CRATE_NAME, start) - .map_err_into(TTEK::StoreIdError) + .chain_err(|| TTEK::StoreIdError) .map(|mut id| { id.local_push(PathBuf::from(ts.as_str())); id }) - .and_then(|id| self.create(id).map_err_into(TTEK::StoreWriteError)) + .and_then(|id| self.create(id).chain_err(|| TTEK::StoreWriteError)) .and_then(|mut fle| { let v = Value::String(ts.as_str().to_owned()); fle.get_header_mut() .insert(DATE_TIME_TAG_HEADER_PATH, v) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| fle) }) .and_then(|mut fle| { let v = Value::String(start.format(DATE_TIME_FORMAT).to_string()); fle.get_header_mut() .insert(DATE_TIME_START_HEADER_PATH, v) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| fle) }) } @@ -99,14 +99,14 @@ impl<'a> TimeTrackStore<'a> for Store { let v = Value::String(end.format(DATE_TIME_FORMAT).to_string()); fle.get_header_mut() .insert(DATE_TIME_END_HEADER_PATH, v) - .map_err_into(TTEK::HeaderWriteError) + .chain_err(|| TTEK::HeaderWriteError) .map(|_| fle) }) } fn get_timetrackings(&'a self) -> Result> { self.retrieve_for_module(CRATE_NAME) - .map_err_into(TTEK::StoreReadError) + .chain_err(|| TTEK::StoreReadError) .map(|iter| GetTimeTrackIter::new(iter, self)) }