From 06e78c75de33a547101f08fc96a8f089c8ec492f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 18 May 2019 00:14:34 +0200 Subject: [PATCH] Add more context in error messages Signed-off-by: Matthias Beyer --- .../libimagtimetrack/src/iter/create.rs | 2 ++ .../libimagtimetrack/src/timetracking.rs | 20 ++++++++++++++++++- .../libimagtimetrack/src/timetrackingstore.rs | 3 +++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/domain/libimagtimetrack/src/iter/create.rs b/lib/domain/libimagtimetrack/src/iter/create.rs index 82519036..d08fc253 100644 --- a/lib/domain/libimagtimetrack/src/iter/create.rs +++ b/lib/domain/libimagtimetrack/src/iter/create.rs @@ -21,6 +21,7 @@ use toml::Value; use toml_query::insert::TomlValueInsertExt; use chrono::naive::NaiveDateTime as NDT; use failure::Fallible as Result; +use failure::ResultExt; use failure::Error; use crate::constants::*; @@ -60,6 +61,7 @@ impl<'a> Iterator for CreateTimeTrackIter<'a> res.and_then(|(id, starttime)| { self.store .create(id) + .context("Failed to create entry") .map_err(Error::from) .and_then(|mut entry| { let v = Value::String(starttime.format(DATE_TIME_FORMAT).to_string()); diff --git a/lib/domain/libimagtimetrack/src/timetracking.rs b/lib/domain/libimagtimetrack/src/timetracking.rs index 045725f2..f47be7f7 100644 --- a/lib/domain/libimagtimetrack/src/timetracking.rs +++ b/lib/domain/libimagtimetrack/src/timetracking.rs @@ -37,6 +37,7 @@ use toml_query::delete::TomlValueDeleteExt; use toml_query::insert::TomlValueInsertExt; use toml_query::read::TomlValueReadTypeExt; use failure::Fallible as Result; +use failure::ResultExt; use failure::Error; pub trait TimeTracking { @@ -64,6 +65,8 @@ impl TimeTracking for Entry { fn get_timetrack_tag(&self) -> Result { self.get_header() .read_string(DATE_TIME_TAG_HEADER_PATH) + .context(format_err!("Failed to read header '{}' of {}", DATE_TIME_TAG_HEADER_PATH, + self.get_location())) .map_err(Error::from)? .ok_or_else(|| Error::from(EM::EntryHeaderReadError)) .map(Into::into) @@ -74,6 +77,8 @@ impl TimeTracking for Entry { self.get_header_mut() .insert(DATE_TIME_START_HEADER_PATH, Value::String(s)) + .context(format_err!("Failed get header '{}' of {}", DATE_TIME_START_HEADER_PATH, + self.get_location())) .map_err(Error::from) .map(|_| ()) } @@ -81,6 +86,8 @@ impl TimeTracking for Entry { fn get_start_datetime(&self) -> Result> { self.get_header() .read_string(DATE_TIME_START_HEADER_PATH) + .context(format_err!("Failed read header '{}' of {}", DATE_TIME_START_HEADER_PATH, + self.get_location())) .map_err(Error::from) .and_then(header_value_to_dt) } @@ -88,6 +95,8 @@ impl TimeTracking for Entry { fn delete_start_datetime(&mut self) -> Result<()> { self.get_header_mut() .delete(DATE_TIME_START_HEADER_PATH) + .context(format_err!("Failed delete header '{}' of {}", DATE_TIME_START_HEADER_PATH, + self.get_location())) .map_err(Error::from) .map(|_| ()) } @@ -97,6 +106,8 @@ impl TimeTracking for Entry { self.get_header_mut() .insert(DATE_TIME_END_HEADER_PATH, Value::String(s)) + .context(format_err!("Failed insert header '{}' in {}", DATE_TIME_END_HEADER_PATH, + self.get_location())) .map_err(Error::from) .map(|_| ()) } @@ -104,6 +115,8 @@ impl TimeTracking for Entry { fn get_end_datetime(&self) -> Result> { self.get_header() .read_string(DATE_TIME_END_HEADER_PATH) + .context(format_err!("Failed read header '{}' of {}", DATE_TIME_END_HEADER_PATH, + self.get_location())) .map_err(Error::from) .and_then(header_value_to_dt) } @@ -111,6 +124,8 @@ impl TimeTracking for Entry { fn delete_end_datetime(&mut self) -> Result<()> { self.get_header_mut() .delete(DATE_TIME_END_HEADER_PATH) + .context(format_err!("Failed delete header '{}' of {}", DATE_TIME_END_HEADER_PATH, + self.get_location())) .map_err(Error::from) .map(|_| ()) } @@ -135,7 +150,10 @@ impl TimeTracking for Entry { fn header_value_to_dt(val: Option) -> Result> { match val { - Some(ref s) => NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT).map_err(Error::from).map(Some), + Some(ref s) => NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT) + .context(format_err!("Failed to parse '{}' datetime with format '{}'", + s, DATE_TIME_FORMAT)) + .map_err(Error::from).map(Some), None => Ok(None), } } diff --git a/lib/domain/libimagtimetrack/src/timetrackingstore.rs b/lib/domain/libimagtimetrack/src/timetrackingstore.rs index 2144185b..967c8958 100644 --- a/lib/domain/libimagtimetrack/src/timetrackingstore.rs +++ b/lib/domain/libimagtimetrack/src/timetrackingstore.rs @@ -26,6 +26,7 @@ use chrono::NaiveDateTime as NDT; use toml::Value; use toml_query::insert::TomlValueInsertExt; use failure::Fallible as Result; +use failure::ResultExt; use failure::Error; use libimagstore::store::Store; @@ -70,6 +71,8 @@ impl<'a> TimeTrackStore<'a> for Store { use std::path::PathBuf; COMPILER.compile(CRATE_NAME, start) + .context(format_err!("Failed to compile DatePath for crate '{}' with start = '{}'", + CRATE_NAME, start)) .map_err(Error::from) .map(|mut id| { id.local_push(PathBuf::from(ts.as_str()));