libimagtimetrack: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 16:23:45 +02:00
parent 22cff91653
commit a5bb7a9bec
8 changed files with 41 additions and 32 deletions

View file

@ -17,12 +17,26 @@
// 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 {
TimeTrackError, TimeTrackErrorKind, ResultExt, Result; TimeTrackError, TimeTrackErrorKind, ResultExt, Result;
} }
errors { errors {
StoreReadError {
description("Error while writing Store")
display("Error while writing Store")
}
StoreWriteError {
description("Error while reading Store")
display("Error while reading Store")
}
StoreIdError { StoreIdError {
description("Error while handling StoreId") description("Error while handling StoreId")
display("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 { impl IntoError for TimeTrackErrorKind {
type Target = TimeTrackError; type Target = TimeTrackError;
@ -63,7 +73,7 @@ impl IntoError for TimeTrackErrorKind {
TimeTrackError::from_kind(self) TimeTrackError::from_kind(self)
} }
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target { fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
TimeTrackError::from_kind(self) TimeTrackError::from_kind(self)
} }
} }

View file

@ -24,7 +24,7 @@ use chrono::naive::NaiveDateTime as NDT;
use constants::*; use constants::*;
use error::TimeTrackError as TTE; use error::TimeTrackError as TTE;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use iter::storeid::TagStoreIdIter; use iter::storeid::TagStoreIdIter;
use iter::setendtime::SetEndTimeIter; use iter::setendtime::SetEndTimeIter;
@ -61,12 +61,12 @@ impl<'a> Iterator for CreateTimeTrackIter<'a>
res.and_then(|(id, starttime)| { res.and_then(|(id, starttime)| {
self.store self.store
.create(id) .create(id)
.map_err_into(TTEK::StoreWriteError) .chain_err(|| TTEK::StoreWriteError)
.and_then(|mut entry| { .and_then(|mut entry| {
let v = Value::String(starttime.format(DATE_TIME_FORMAT).to_string()); let v = Value::String(starttime.format(DATE_TIME_FORMAT).to_string());
entry.get_header_mut() entry.get_header_mut()
.insert(DATE_TIME_START_HEADER_PATH, v) .insert(DATE_TIME_START_HEADER_PATH, v)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| entry) .map(|_| entry)
}) })
}) })

View file

@ -19,7 +19,7 @@
use error::TimeTrackError as TTE; use error::TimeTrackError as TTE;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::store::Store; use libimagstore::store::Store;
@ -46,7 +46,7 @@ impl<'a> Iterator for GetTimeTrackIter<'a> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|sid| { 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(None) => Err(TTEK::StoreReadError.into_error()),
Ok(Some(s)) => Ok(s), Ok(Some(s)) => Ok(s),
Err(e) => Err(e) Err(e) => Err(e)

View file

@ -24,7 +24,7 @@ use chrono::naive::NaiveDateTime as NDT;
use constants::*; use constants::*;
use error::TimeTrackError as TTE; use error::TimeTrackError as TTE;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use iter::create::CreateTimeTrackIter; use iter::create::CreateTimeTrackIter;
use libimagstore::store::FileLockEntry; 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()); let v = Value::String(self.datetime.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut() fle.get_header_mut()
.insert(DATE_TIME_END_HEADER_PATH, v) .insert(DATE_TIME_END_HEADER_PATH, v)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| fle) .map(|_| fle)
}) })
}) })

View file

@ -22,7 +22,7 @@ use chrono::naive::NaiveDateTime as NDT;
use constants::*; use constants::*;
use error::TimeTrackError; use error::TimeTrackError;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use iter::tag::TagIter; use iter::tag::TagIter;
use iter::create::CreateTimeTrackIter; use iter::create::CreateTimeTrackIter;
@ -64,7 +64,7 @@ impl Iterator for TagStoreIdIter {
let id_str = format!("{}-{}", dt, tag.as_str()); let id_str = format!("{}-{}", dt, tag.as_str());
ModuleEntryPath::new(id_str) ModuleEntryPath::new(id_str)
.into_storeid() .into_storeid()
.map_err_into(TTEK::StoreIdError) .chain_err(|| TTEK::StoreIdError)
.map(|id| (id, self.datetime.clone())) .map(|id| (id, self.datetime.clone()))
}) })
}) })

View file

@ -45,12 +45,11 @@ extern crate lazy_static;
extern crate is_match; extern crate is_match;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
#[macro_use]
extern crate libimagerror;
#[macro_use] #[macro_use]
extern crate libimagstore; extern crate libimagstore;
extern crate libimagentrydatetime; extern crate libimagentrydatetime;
extern crate libimagentrytag; extern crate libimagentrytag;
extern crate libimagerror;
mod constants; mod constants;
pub mod error; pub mod error;

View file

@ -31,7 +31,7 @@ use libimagerror::into::IntoError;
use tag::TimeTrackingTag as TTT; use tag::TimeTrackingTag as TTT;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use result::Result; use result::Result;
use constants::*; use constants::*;
@ -65,7 +65,7 @@ impl TimeTracking for Entry {
fn get_timetrack_tag(&self) -> Result<TTT> { fn get_timetrack_tag(&self) -> Result<TTT> {
self.get_header() self.get_header()
.read(DATE_TIME_TAG_HEADER_PATH) .read(DATE_TIME_TAG_HEADER_PATH)
.map_err_into(TTEK::HeaderReadError) .chain_err(|| TTEK::HeaderReadError)
.and_then(|value| match value { .and_then(|value| match value {
Some(&Value::String(ref s)) => Ok(s.clone().into()), Some(&Value::String(ref s)) => Ok(s.clone().into()),
Some(_) => Err(TTEK::HeaderFieldTypeError.into_error()), Some(_) => Err(TTEK::HeaderFieldTypeError.into_error()),
@ -78,21 +78,21 @@ impl TimeTracking for Entry {
self.get_header_mut() self.get_header_mut()
.insert(DATE_TIME_START_HEADER_PATH, Value::String(s)) .insert(DATE_TIME_START_HEADER_PATH, Value::String(s))
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| ()) .map(|_| ())
} }
fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>> { fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>> {
self.get_header() self.get_header()
.read(DATE_TIME_START_HEADER_PATH) .read(DATE_TIME_START_HEADER_PATH)
.map_err_into(TTEK::HeaderReadError) .chain_err(|| TTEK::HeaderReadError)
.and_then(header_value_to_dt) .and_then(header_value_to_dt)
} }
fn delete_start_datetime(&mut self) -> Result<()> { fn delete_start_datetime(&mut self) -> Result<()> {
self.get_header_mut() self.get_header_mut()
.delete(DATE_TIME_START_HEADER_PATH) .delete(DATE_TIME_START_HEADER_PATH)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| ()) .map(|_| ())
} }
@ -101,21 +101,21 @@ impl TimeTracking for Entry {
self.get_header_mut() self.get_header_mut()
.insert(DATE_TIME_END_HEADER_PATH, Value::String(s)) .insert(DATE_TIME_END_HEADER_PATH, Value::String(s))
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| ()) .map(|_| ())
} }
fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>> { fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>> {
self.get_header() self.get_header()
.read(DATE_TIME_END_HEADER_PATH) .read(DATE_TIME_END_HEADER_PATH)
.map_err_into(TTEK::HeaderReadError) .chain_err(|| TTEK::HeaderReadError)
.and_then(header_value_to_dt) .and_then(header_value_to_dt)
} }
fn delete_end_datetime(&mut self) -> Result<()> { fn delete_end_datetime(&mut self) -> Result<()> {
self.get_header_mut() self.get_header_mut()
.delete(DATE_TIME_END_HEADER_PATH) .delete(DATE_TIME_END_HEADER_PATH)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| ()) .map(|_| ())
} }
@ -141,7 +141,7 @@ fn header_value_to_dt(val: Option<&Value>) -> Result<Option<NaiveDateTime>> {
match val { match val {
Some(&Value::String(ref s)) => { Some(&Value::String(ref s)) => {
NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT) NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT)
.map_err_into(TTEK::DateTimeParserError) .chain_err(|| TTEK::DateTimeParserError)
.map(Some) .map(Some)
}, },

View file

@ -33,7 +33,7 @@ use libimagentrydatetime::datepath::compiler::DatePathCompiler;
use result::Result; use result::Result;
use constants::*; use constants::*;
use error::TimeTrackErrorKind as TTEK; use error::TimeTrackErrorKind as TTEK;
use error::MapErrInto; use error::ResultExt;
use iter::get::GetTimeTrackIter; use iter::get::GetTimeTrackIter;
use tag::TimeTrackingTag as TTT; use tag::TimeTrackingTag as TTT;
@ -71,24 +71,24 @@ impl<'a> TimeTrackStore<'a> for Store {
use std::path::PathBuf; use std::path::PathBuf;
COMPILER.compile(CRATE_NAME, start) COMPILER.compile(CRATE_NAME, start)
.map_err_into(TTEK::StoreIdError) .chain_err(|| TTEK::StoreIdError)
.map(|mut id| { .map(|mut id| {
id.local_push(PathBuf::from(ts.as_str())); id.local_push(PathBuf::from(ts.as_str()));
id 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| { .and_then(|mut fle| {
let v = Value::String(ts.as_str().to_owned()); let v = Value::String(ts.as_str().to_owned());
fle.get_header_mut() fle.get_header_mut()
.insert(DATE_TIME_TAG_HEADER_PATH, v) .insert(DATE_TIME_TAG_HEADER_PATH, v)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| fle) .map(|_| fle)
}) })
.and_then(|mut fle| { .and_then(|mut fle| {
let v = Value::String(start.format(DATE_TIME_FORMAT).to_string()); let v = Value::String(start.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut() fle.get_header_mut()
.insert(DATE_TIME_START_HEADER_PATH, v) .insert(DATE_TIME_START_HEADER_PATH, v)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| fle) .map(|_| fle)
}) })
} }
@ -99,14 +99,14 @@ impl<'a> TimeTrackStore<'a> for Store {
let v = Value::String(end.format(DATE_TIME_FORMAT).to_string()); let v = Value::String(end.format(DATE_TIME_FORMAT).to_string());
fle.get_header_mut() fle.get_header_mut()
.insert(DATE_TIME_END_HEADER_PATH, v) .insert(DATE_TIME_END_HEADER_PATH, v)
.map_err_into(TTEK::HeaderWriteError) .chain_err(|| TTEK::HeaderWriteError)
.map(|_| fle) .map(|_| fle)
}) })
} }
fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>> { fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>> {
self.retrieve_for_module(CRATE_NAME) self.retrieve_for_module(CRATE_NAME)
.map_err_into(TTEK::StoreReadError) .chain_err(|| TTEK::StoreReadError)
.map(|iter| GetTimeTrackIter::new(iter, self)) .map(|iter| GetTimeTrackIter::new(iter, self))
} }