From 6e0a179550b0b9c2bce65e45f93cac03ac3e1b0f 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 --- lib/entry/libimagentrycategory/src/category.rs | 2 ++ lib/entry/libimagentrycategory/src/entry.rs | 6 +++--- lib/entry/libimagentrycategory/src/iter.rs | 12 +++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/entry/libimagentrycategory/src/category.rs b/lib/entry/libimagentrycategory/src/category.rs index de81bfbd..fb7268a7 100644 --- a/lib/entry/libimagentrycategory/src/category.rs +++ b/lib/entry/libimagentrycategory/src/category.rs @@ -27,6 +27,7 @@ use libimagentrylink::internal::InternalLinker; use toml_query::read::TomlValueReadTypeExt; use failure::Fallible as Result; +use failure::ResultExt; use failure::Error; use failure::err_msg; use crate::store::CATEGORY_REGISTER_NAME_FIELD_PATH; @@ -49,6 +50,7 @@ impl Category for Entry { trace!("Getting category name of '{:?}'", self.get_location()); self.get_header() .read_string(CATEGORY_REGISTER_NAME_FIELD_PATH) + .context(format_err!("Failed to read header at '{}'", CATEGORY_REGISTER_NAME_FIELD_PATH)) .map_err(Error::from)? .ok_or_else(|| Error::from(err_msg("Category name missing"))) } diff --git a/lib/entry/libimagentrycategory/src/entry.rs b/lib/entry/libimagentrycategory/src/entry.rs index 8260de8a..9876add4 100644 --- a/lib/entry/libimagentrycategory/src/entry.rs +++ b/lib/entry/libimagentrycategory/src/entry.rs @@ -52,7 +52,7 @@ impl EntryCategory for Entry { trace!("Setting category '{}' UNCHECKED", s); self.get_header_mut() .insert(&String::from("category.value"), Value::String(s.to_string())) - .map_err(Error::from) + .context(format_err!("Failed to insert header at 'category.value' of '{}'", self.get_location())) .context(EM::EntryHeaderWriteError) .map_err(Error::from) .map(|_| ()) @@ -84,7 +84,7 @@ impl EntryCategory for Entry { trace!("Has category? '{}'", self.get_location()); self.get_header() .read("category.value") - .map_err(Error::from) + .context(format_err!("Failed to read header at 'category.value' of '{}'", self.get_location())) .context(EM::EntryHeaderReadError) .map_err(Error::from) .map(|x| x.is_some()) @@ -101,7 +101,7 @@ impl EntryCategory for Entry { self.get_header_mut() .delete("category.value") - .map_err(Error::from) + .context(format_err!("Failed to delete header at 'category.value' of '{}'", self.get_location())) .context(EM::EntryHeaderWriteError) .map_err(Error::from) .map(|_| ()) diff --git a/lib/entry/libimagentrycategory/src/iter.rs b/lib/entry/libimagentrycategory/src/iter.rs index a16f0b9b..7ed342c7 100644 --- a/lib/entry/libimagentrycategory/src/iter.rs +++ b/lib/entry/libimagentrycategory/src/iter.rs @@ -20,7 +20,6 @@ use libimagstore::storeid::StoreIdIterator; use libimagstore::store::Store; use libimagstore::store::FileLockEntry; -use libimagerror::errors::ErrorMsg as EM; use toml_query::read::TomlValueReadTypeExt; @@ -60,8 +59,8 @@ impl<'a> Iterator for CategoryNameIter<'a> { let query = CATEGORY_REGISTER_NAME_FIELD_PATH; while let Some(sid) = self.1.next() { - match sid { - Err(e) => return Some(Err(e).map_err(Error::from)), + match sid.context("Error while iterating over category names").map_err(Error::from) { + Err(e) => return Some(Err(e)), Ok(sid) => { if sid.is_in_collection(&["category"]) { let func = |store: &Store| { // hack for returning Some(Result<_, _>) @@ -70,8 +69,7 @@ impl<'a> Iterator for CategoryNameIter<'a> { .ok_or_else(|| err_msg("Store read error"))? .get_header() .read_string(query) - .map_err(Error::from) - .context(EM::EntryHeaderReadError)? + .context(format_err!("Failed to read header at '{}'", query))? .ok_or_else(|| err_msg("Store read error")) .map_err(Error::from) }; @@ -99,8 +97,8 @@ impl<'a> Iterator for CategoryEntryIterator<'a> { fn next(&mut self) -> Option { while let Some(next) = self.1.next() { - match next { - Err(e) => return Some(Err(e).map_err(Error::from)), + match next.context("Error while iterating over category entries").map_err(Error::from) { + Err(e) => return Some(Err(e)), Ok(next) => { let getter = |next| -> Result<(String, FileLockEntry<'a>)> { let entry = self.0