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
//
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<Error>) -> Self::Target {
fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
TimeTrackError::from_kind(self)
}
}

View file

@ -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)
})
})

View file

@ -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::Item> {
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)

View file

@ -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)
})
})

View file

@ -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()))
})
})

View file

@ -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;

View file

@ -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<TTT> {
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<Option<NaiveDateTime>> {
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<Option<NaiveDateTime>> {
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<Option<NaiveDateTime>> {
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)
},

View file

@ -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<GetTimeTrackIter<'a>> {
self.retrieve_for_module(CRATE_NAME)
.map_err_into(TTEK::StoreReadError)
.chain_err(|| TTEK::StoreReadError)
.map(|iter| GetTimeTrackIter::new(iter, self))
}