diff options
-rw-r--r-- | libimagstore/src/error.rs | 12 | ||||
-rw-r--r-- | libimagstore/src/store.rs | 75 |
2 files changed, 72 insertions, 15 deletions
diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index e14ed119..6432e0f3 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -32,7 +32,17 @@ generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData, StorePathLacksVersion => "The supplied store path has no version part", GlobError => "glob() error", EncodingError => "Encoding error", - StorePathError => "Store Path error" + StorePathError => "Store Path error", + + CreateCallError => "Error when calling create()", + RetrieveCallError => "Error when calling retrieve()", + GetCallError => "Error when calling get()", + GetAllVersionsCallError => "Error when calling get_all_versions()", + RetrieveForModuleCallError => "Error when calling retrieve_for_module()", + UpdateCallError => "Error when calling update()", + RetrieveCopyCallError => "Error when calling retrieve_copy()", + DeleteCallError => "Error when calling delete()" + ); generate_custom_error_types!(ParserError, ParserErrorKind, CustomErrorData, diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 0bc3d444..be0652ff 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -319,18 +319,24 @@ impl Store { return Err(e) .map_err(Box::new) .map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) + .map_err(Box::new) + .map_err(|e| SEK::CreateCallError.into_error_with_cause(e)) } else { trace_error(&e); } } let mut hsmap = match self.entries.write() { - Err(_) => return Err(SE::new(SEK::LockPoisoned, None)), + Err(_) => return Err(SE::new(SEK::LockPoisoned, None)) + .map_err(Box::new) + .map_err(|e| SEK::CreateCallError.into_error_with_cause(e)), Ok(s) => s, }; if hsmap.contains_key(&id) { return Err(SE::new(SEK::EntryAlreadyExists, None)) + .map_err(Box::new) + .map_err(|e| SEK::CreateCallError.into_error_with_cause(e)) } hsmap.insert(id.clone(), { let mut se = StoreEntry::new(id.clone()); @@ -342,6 +348,8 @@ impl Store { self.execute_hooks_for_mut_file(self.post_create_aspects.clone(), &mut fle) .map_err(|e| SE::new(SEK::PostHookExecuteError, Some(Box::new(e)))) .map(|_| fle) + .map_err(Box::new) + .map_err(|e| SEK::CreateCallError.into_error_with_cause(e)) } /// Borrow a given Entry. When the `FileLockEntry` is either `update`d or @@ -356,6 +364,8 @@ impl Store { return Err(e) .map_err(Box::new) .map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) + .map_err(Box::new) + .map_err(|e| SEK::RetrieveCallError.into_error_with_cause(e)) } else { trace_error(&e); } @@ -376,6 +386,8 @@ impl Store { .map_err(|e| SE::new(SEK::HookExecutionError, Some(Box::new(e)))) .and(Ok(fle)) }) + .map_err(Box::new) + .map_err(|e| SEK::RetrieveCallError.into_error_with_cause(e)) } /// Get an entry from the store if it exists. @@ -387,6 +399,8 @@ impl Store { return Ok(None); } self.retrieve(id).map(Some) + .map_err(Box::new) + .map_err(|e| SEK::GetCallError.into_error_with_cause(e)) } /// Same as `Store::get()` but also tries older versions of the entry, returning an iterator @@ -401,13 +415,17 @@ impl Store { let mut split = s.split("~"); let path_element = match split.next() { Some(s) => s, - None => return Err(SE::new(SEK::StorePathError, None)), + None => return Err(SE::new(SEK::StorePathError, None)) + .map_err(Box::new) + .map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)), }; Ok(PathBuf::from(path_element)) }, - None => Err(SE::new(SEK::StorePathError, None)), + None => Err(SE::new(SEK::StorePathError, None)) + .map_err(Box::new) + .map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)), } } @@ -417,12 +435,18 @@ impl Store { } match path_component(id).map(build_glob_pattern) { - Err(e) => Err(SE::new(SEK::StorePathError, Some(Box::new(e)))), - Ok(None) => Err(SE::new(SEK::StorePathError, None)), + Err(e) => Err(SE::new(SEK::StorePathError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)), + Ok(None) => Err(SE::new(SEK::StorePathError, None)) + .map_err(Box::new) + .map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)), Ok(Some(pattern)) => { glob(&pattern[..]) .map(|paths| GlobStoreIdIterator::new(paths).into()) .map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)) } } @@ -442,6 +466,8 @@ impl Store { }) .map(|paths| GlobStoreIdIterator::new(paths).into()) .map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::RetrieveForModuleCallError.into_error_with_cause(e)) } // Walk the store tree for the module @@ -456,17 +482,23 @@ impl Store { return Err(e) .map_err(Box::new) .map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) + .map_err(Box::new) + .map_err(|e| SEK::UpdateCallError.into_error_with_cause(e)); } else { trace_error(&e); } } if let Err(e) = self._update(&entry) { - return Err(e); + return Err(e) + .map_err(Box::new) + .map_err(|e| SEK::UpdateCallError.into_error_with_cause(e)); } self.execute_hooks_for_mut_file(self.post_update_aspects.clone(), &mut entry) .map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::UpdateCallError.into_error_with_cause(e)) } /// Internal method to write to the filesystem store. @@ -499,13 +531,19 @@ impl Store { pub fn retrieve_copy<S: IntoStoreId>(&self, id: S) -> Result<Entry> { let id = self.storify_id(id.into_storeid()); let entries = match self.entries.write() { - Err(_) => return Err(SE::new(SEK::LockPoisoned, None)), + Err(_) => { + return Err(SE::new(SEK::LockPoisoned, None)) + .map_err(Box::new) + .map_err(|e| SEK::RetrieveCopyCallError.into_error_with_cause(e)); + }, Ok(e) => e, }; // if the entry is currently modified by the user, we cannot drop it if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) { - return Err(SE::new(SEK::IdLocked, None)); + return Err(SE::new(SEK::IdLocked, None)) + .map_err(Box::new) + .map_err(|e| SEK::RetrieveCopyCallError.into_error_with_cause(e)); } StoreEntry::new(id).get_entry() @@ -516,32 +554,41 @@ impl Store { let id = self.storify_id(id.into_storeid()); if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) { if e.is_aborting() { - return Err(e).map_err(|e| { - SE::new(SEK::PreHookExecuteError, Some(Box::new(e))) - }) + return Err(e) + .map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)) } else { trace_error(&e); } } let mut entries = match self.entries.write() { - Err(_) => return Err(SE::new(SEK::LockPoisoned, None)), + Err(_) => return Err(SE::new(SEK::LockPoisoned, None)) + .map_err(Box::new) + .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)), Ok(e) => e, }; // if the entry is currently modified by the user, we cannot drop it if entries.get(&id).map(|e| e.is_borrowed()).unwrap_or(false) { - return Err(SE::new(SEK::IdLocked, None)); + return Err(SE::new(SEK::IdLocked, None)) + .map_err(Box::new) + .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)); } // remove the entry first, then the file entries.remove(&id); if let Err(e) = remove_file(&id) { - return Err(SE::new(SEK::FileError, Some(Box::new(e)))); + return Err(SE::new(SEK::FileError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)); } self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id) .map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) + .map_err(Box::new) + .map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)) } /// Gets the path where this store is on the disk |