store.rs: Replace error boxing and wrapping with result helper from libimagerror

This commit is contained in:
Matthias Beyer 2016-06-27 18:16:43 +02:00
parent 484520842a
commit 45a24de853

View file

@ -24,6 +24,7 @@ use walkdir::Iter as WalkDirIter;
use error::{ParserErrorKind, ParserError}; use error::{ParserErrorKind, ParserError};
use error::{StoreError as SE, StoreErrorKind as SEK}; use error::{StoreError as SE, StoreErrorKind as SEK};
use error::MapErrInto;
use storeid::{IntoStoreId, StoreId, StoreIdIterator}; use storeid::{IntoStoreId, StoreId, StoreIdIterator};
use lazyfile::LazyFile; use lazyfile::LazyFile;
@ -153,10 +154,8 @@ impl StoreEntry {
let file = try!(self.file.create_file()); let file = try!(self.file.create_file());
assert_eq!(self.id, entry.location); assert_eq!(self.id, entry.location);
try!(file.set_len(0) try!(file.set_len(0).map_err_into(SEK::FileError));
.map_err(|e| SE::new(SEK::FileError, Some(Box::new(e))))); file.write_all(entry.to_str().as_bytes()).map_err_into(SEK::FileError)
file.write_all(entry.to_str().as_bytes())
.map_err(|e| SE::new(SEK::FileError, Some(Box::new(e))))
} else { } else {
Ok(()) Ok(())
} }
@ -217,12 +216,11 @@ impl Store {
let c = create_dir_all(location.clone()); let c = create_dir_all(location.clone());
if c.is_err() { if c.is_err() {
debug!("Failed"); debug!("Failed");
return Err(SE::new(SEK::StorePathCreate, return Err(SEK::StorePathCreate.into_error_with_cause(Box::new(c.unwrap_err())));
Some(Box::new(c.unwrap_err()))));
} }
} else if location.is_file() { } else if location.is_file() {
debug!("Store path exists as file"); debug!("Store path exists as file");
return Err(SE::new(SEK::StorePathExists, None)); return Err(SEK::StorePathExists.into_error());
} }
let store_unload_aspects = get_store_unload_aspect_names(&store_config) let store_unload_aspects = get_store_unload_aspect_names(&store_config)
@ -333,26 +331,20 @@ impl Store {
if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) { if let Err(e) = self.execute_hooks_for_id(self.pre_create_aspects.clone(), &id) {
if e.is_aborting() { if e.is_aborting() {
return Err(e) return Err(e)
.map_err(Box::new) .map_err_into(SEK::PreHookExecuteError)
.map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) .map_err_into(SEK::CreateCallError)
.map_err(Box::new)
.map_err(|e| SEK::CreateCallError.into_error_with_cause(e))
} else { } else {
trace_error(&e); trace_error(&e);
} }
} }
let mut hsmap = match self.entries.write() { let mut hsmap = match self.entries.write() {
Err(_) => return Err(SE::new(SEK::LockPoisoned, None)) Err(_) => return Err(SEK::LockPoisoned.into_error()).map_err_into(SEK::CreateCallError),
.map_err(Box::new)
.map_err(|e| SEK::CreateCallError.into_error_with_cause(e)),
Ok(s) => s, Ok(s) => s,
}; };
if hsmap.contains_key(&id) { if hsmap.contains_key(&id) {
return Err(SE::new(SEK::EntryAlreadyExists, None)) return Err(SEK::EntryAlreadyExists.into_error()).map_err_into(SEK::CreateCallError);
.map_err(Box::new)
.map_err(|e| SEK::CreateCallError.into_error_with_cause(e))
} }
hsmap.insert(id.clone(), { hsmap.insert(id.clone(), {
let mut se = StoreEntry::new(id.clone()); let mut se = StoreEntry::new(id.clone());
@ -362,10 +354,9 @@ impl Store {
let mut fle = FileLockEntry::new(self, Entry::new(id)); let mut fle = FileLockEntry::new(self, Entry::new(id));
self.execute_hooks_for_mut_file(self.post_create_aspects.clone(), &mut fle) 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_err_into(SEK::PostHookExecuteError)
.map(|_| fle) .map(|_| fle)
.map_err(Box::new) .map_err_into(SEK::CreateCallError)
.map_err(|e| SEK::CreateCallError.into_error_with_cause(e))
} }
/// Borrow a given Entry. When the `FileLockEntry` is either `update`d or /// Borrow a given Entry. When the `FileLockEntry` is either `update`d or
@ -378,10 +369,8 @@ impl Store {
if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) { if let Err(e) = self.execute_hooks_for_id(self.pre_retrieve_aspects.clone(), &id) {
if e.is_aborting() { if e.is_aborting() {
return Err(e) return Err(e)
.map_err(Box::new) .map_err_into(SEK::PreHookExecuteError)
.map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) .map_err_into(SEK::RetrieveCallError)
.map_err(Box::new)
.map_err(|e| SEK::RetrieveCallError.into_error_with_cause(e))
} else { } else {
trace_error(&e); trace_error(&e);
} }
@ -399,11 +388,10 @@ impl Store {
.map(|e| FileLockEntry::new(self, e)) .map(|e| FileLockEntry::new(self, e))
.and_then(|mut fle| { .and_then(|mut fle| {
self.execute_hooks_for_mut_file(self.post_retrieve_aspects.clone(), &mut fle) self.execute_hooks_for_mut_file(self.post_retrieve_aspects.clone(), &mut fle)
.map_err(|e| SE::new(SEK::HookExecutionError, Some(Box::new(e)))) .map_err_into(SEK::HookExecutionError)
.and(Ok(fle)) .and(Ok(fle))
}) })
.map_err(Box::new) .map_err_into(SEK::RetrieveCallError)
.map_err(|e| SEK::RetrieveCallError.into_error_with_cause(e))
} }
/// Get an entry from the store if it exists. /// Get an entry from the store if it exists.
@ -414,9 +402,7 @@ impl Store {
debug!("Does not exist: {:?}", id.clone().into_storeid()); debug!("Does not exist: {:?}", id.clone().into_storeid());
return Ok(None); return Ok(None);
} }
self.retrieve(id).map(Some) self.retrieve(id).map(Some).map_err_into(SEK::GetCallError)
.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 /// Same as `Store::get()` but also tries older versions of the entry, returning an iterator
@ -432,16 +418,14 @@ impl Store {
let path_element = match split.next() { let path_element = match split.next() {
Some(s) => s, 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_into(SEK::GetAllVersionsCallError),
.map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)),
}; };
Ok(PathBuf::from(path_element)) 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_into(SEK::GetAllVersionsCallError),
.map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)),
} }
} }
@ -451,18 +435,15 @@ impl Store {
} }
match path_component(id).map(build_glob_pattern) { match path_component(id).map(build_glob_pattern) {
Err(e) => Err(SE::new(SEK::StorePathError, Some(Box::new(e)))) Err(e) => Err(SEK::StorePathError.into_error_with_cause(Box::new(e)))
.map_err(Box::new) .map_err_into(SEK::GetAllVersionsCallError),
.map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)),
Ok(None) => Err(SE::new(SEK::StorePathError, None)) Ok(None) => Err(SE::new(SEK::StorePathError, None))
.map_err(Box::new) .map_err_into(SEK::GetAllVersionsCallError),
.map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e)),
Ok(Some(pattern)) => { Ok(Some(pattern)) => {
glob(&pattern[..]) glob(&pattern[..])
.map(|paths| GlobStoreIdIterator::new(paths).into()) .map(|paths| GlobStoreIdIterator::new(paths).into())
.map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e)))) .map_err_into(SEK::GlobError)
.map_err(Box::new) .map_err_into(SEK::GetAllVersionsCallError)
.map_err(|e| SEK::GetAllVersionsCallError.into_error_with_cause(e))
} }
} }
@ -478,12 +459,11 @@ impl Store {
.and_then(|path| { .and_then(|path| {
let path = [ path, "/**/*" ].join(""); let path = [ path, "/**/*" ].join("");
debug!("glob()ing with '{}'", path); debug!("glob()ing with '{}'", path);
glob(&path[..]).map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e)))) glob(&path[..]).map_err_into(SEK::GlobError)
}) })
.map(|paths| GlobStoreIdIterator::new(paths).into()) .map(|paths| GlobStoreIdIterator::new(paths).into())
.map_err(|e| SE::new(SEK::GlobError, Some(Box::new(e)))) .map_err_into(SEK::GlobError)
.map_err(Box::new) .map_err_into(SEK::RetrieveForModuleCallError)
.map_err(|e| SEK::RetrieveForModuleCallError.into_error_with_cause(e))
} }
// Walk the store tree for the module // Walk the store tree for the module
@ -496,25 +476,20 @@ impl Store {
if let Err(e) = self.execute_hooks_for_mut_file(self.pre_update_aspects.clone(), &mut entry) { if let Err(e) = self.execute_hooks_for_mut_file(self.pre_update_aspects.clone(), &mut entry) {
if e.is_aborting() { if e.is_aborting() {
return Err(e) return Err(e)
.map_err(Box::new) .map_err_into(SEK::PreHookExecuteError)
.map_err(|e| SEK::PreHookExecuteError.into_error_with_cause(e)) .map_err_into(SEK::UpdateCallError);
.map_err(Box::new)
.map_err(|e| SEK::UpdateCallError.into_error_with_cause(e));
} else { } else {
trace_error(&e); trace_error(&e);
} }
} }
if let Err(e) = self._update(&entry) { if let Err(e) = self._update(&entry) {
return Err(e) return Err(e).map_err_into(SEK::UpdateCallError);
.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) 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_into(SEK::PreHookExecuteError)
.map_err(Box::new) .map_err_into(SEK::UpdateCallError)
.map_err(|e| SEK::UpdateCallError.into_error_with_cause(e))
} }
/// Internal method to write to the filesystem store. /// Internal method to write to the filesystem store.
@ -549,17 +524,14 @@ impl Store {
let entries = match self.entries.write() { let entries = match self.entries.write() {
Err(_) => { Err(_) => {
return Err(SE::new(SEK::LockPoisoned, None)) return Err(SE::new(SEK::LockPoisoned, None))
.map_err(Box::new) .map_err_into(SEK::RetrieveCopyCallError);
.map_err(|e| SEK::RetrieveCopyCallError.into_error_with_cause(e));
}, },
Ok(e) => e, Ok(e) => e,
}; };
// if the entry is currently modified by the user, we cannot drop it // 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) { 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_into(SEK::RetrieveCopyCallError);
.map_err(Box::new)
.map_err(|e| SEK::RetrieveCopyCallError.into_error_with_cause(e));
} }
StoreEntry::new(id).get_entry() StoreEntry::new(id).get_entry()
@ -571,9 +543,8 @@ impl Store {
if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) { if let Err(e) = self.execute_hooks_for_id(self.pre_delete_aspects.clone(), &id) {
if e.is_aborting() { if e.is_aborting() {
return Err(e) return Err(e)
.map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) .map_err_into(SEK::PreHookExecuteError)
.map_err(Box::new) .map_err_into(SEK::DeleteCallError)
.map_err(|e| SEK::DeleteCallError.into_error_with_cause(e))
} else { } else {
trace_error(&e); trace_error(&e);
} }
@ -581,30 +552,26 @@ impl Store {
let mut entries = match self.entries.write() { 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_into(SEK::DeleteCallError),
.map_err(|e| SEK::DeleteCallError.into_error_with_cause(e)),
Ok(e) => e, Ok(e) => e,
}; };
// if the entry is currently modified by the user, we cannot drop it // 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) { 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_into(SEK::DeleteCallError);
.map_err(|e| SEK::DeleteCallError.into_error_with_cause(e));
} }
// remove the entry first, then the file // remove the entry first, then the file
entries.remove(&id); entries.remove(&id);
if let Err(e) = remove_file(&id) { if let Err(e) = remove_file(&id) {
return Err(SE::new(SEK::FileError, Some(Box::new(e)))) return Err(SEK::FileError.into_error_with_cause(Box::new(e)))
.map_err(Box::new) .map_err_into(SEK::DeleteCallError);
.map_err(|e| SEK::DeleteCallError.into_error_with_cause(e));
} }
self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id) self.execute_hooks_for_id(self.post_delete_aspects.clone(), &id)
.map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) .map_err_into(SEK::PreHookExecuteError)
.map_err(Box::new) .map_err_into(SEK::DeleteCallError)
.map_err(|e| SEK::DeleteCallError.into_error_with_cause(e))
} }
/// Save a copy of the Entry in another place /// Save a copy of the Entry in another place
@ -629,12 +596,10 @@ impl Store {
let new_id = self.storify_id(new_id); let new_id = self.storify_id(new_id);
let hsmap = self.entries.write(); let hsmap = self.entries.write();
if hsmap.is_err() { if hsmap.is_err() {
return Err(SE::new(SEK::LockPoisoned, None)) return Err(SE::new(SEK::LockPoisoned, None)).map_err_into(SEK::MoveCallError)
.map_err(|e| SEK::MoveCallError.into_error_with_cause(Box::new(e)))
} }
if hsmap.unwrap().contains_key(&new_id) { if hsmap.unwrap().contains_key(&new_id) {
return Err(SE::new(SEK::EntryAlreadyExists, None)) return Err(SE::new(SEK::EntryAlreadyExists, None)).map_err_into(SEK::MoveCallError)
.map_err(|e| SEK::MoveCallError.into_error_with_cause(Box::new(e)))
} }
let old_id = entry.get_location().clone(); let old_id = entry.get_location().clone();
@ -647,10 +612,10 @@ impl Store {
Ok(()) Ok(())
} }
}) })
.map_err(|e| SE::new(SEK::FileError, Some(Box::new(e)))) .map_err_into(SEK::FileError)
.and_then(|_| self.execute_hooks_for_id(self.post_move_aspects.clone(), &new_id) .and_then(|_| self.execute_hooks_for_id(self.post_move_aspects.clone(), &new_id)
.map_err(|e| SE::new(SEK::PostHookExecuteError, Some(Box::new(e))))) .map_err_into(SEK::PostHookExecuteError))
.map_err(|e| SEK::MoveCallError.into_error_with_cause(Box::new(e))) .map_err_into(SEK::MoveCallError)
} }
/// Move an entry without loading /// Move an entry without loading
@ -663,9 +628,8 @@ impl Store {
if let Err(e) = self.execute_hooks_for_id(self.pre_move_aspects.clone(), &old_id) { if let Err(e) = self.execute_hooks_for_id(self.pre_move_aspects.clone(), &old_id) {
if e.is_aborting() { if e.is_aborting() {
return Err(e) return Err(e)
.map_err(|e| SE::new(SEK::PreHookExecuteError, Some(Box::new(e)))) .map_err_into(SEK::PreHookExecuteError)
.map_err(Box::new) .map_err_into(SEK::MoveByIdCallError)
.map_err(|e| SEK::MoveByIdCallError.into_error_with_cause(e))
} else { } else {
trace_error(&e); trace_error(&e);
} }
@ -679,10 +643,7 @@ impl Store {
return Err(SE::new(SEK::EntryAlreadyBorrowed, None)); return Err(SE::new(SEK::EntryAlreadyBorrowed, None));
} else { } else {
match rename(old_id, new_id.clone()) { match rename(old_id, new_id.clone()) {
Err(e) => { Err(e) => return Err(SEK::EntryRenameError.into_error_with_cause(Box::new(e))),
let kind = SEK::EntryRenameError;
return Err(SE::new(kind, Some(Box::new(e))));
},
_ => { _ => {
debug!("Rename worked"); debug!("Rename worked");
}, },
@ -690,9 +651,8 @@ impl Store {
} }
self.execute_hooks_for_id(self.pre_move_aspects.clone(), &new_id) self.execute_hooks_for_id(self.pre_move_aspects.clone(), &new_id)
.map_err(|e| SE::new(SEK::PostHookExecuteError, Some(Box::new(e)))) .map_err_into(SEK::PostHookExecuteError)
.map_err(Box::new) .map_err_into(SEK::MoveByIdCallError)
.map_err(|e| SEK::MoveByIdCallError.into_error_with_cause(e))
} }
/// Gets the path where this store is on the disk /// Gets the path where this store is on the disk
@ -724,7 +684,7 @@ impl Store {
}; };
let mut guard = match guard.deref().lock().map_err(|_| SE::new(SEK::LockError, None)) { let mut guard = match guard.deref().lock().map_err(|_| SE::new(SEK::LockError, None)) {
Err(e) => return Err(SE::new(SEK::HookRegisterError, Some(Box::new(e)))), Err(e) => return Err(SEK::HookRegisterError.into_error_with_cause(Box::new(e))),
Ok(g) => g, Ok(g) => g,
}; };
@ -736,8 +696,8 @@ impl Store {
} }
} }
let annfe = SE::new(SEK::AspectNameNotFoundError, None); let annfe = SEK::AspectNameNotFoundError.into_error();
Err(SE::new(SEK::HookRegisterError, Some(Box::new(annfe)))) Err(SEK::HookRegisterError.into_error_with_cause(Box::new(annfe)))
} }
fn get_config_for_hook(&self, name: &str) -> Option<&Value> { fn get_config_for_hook(&self, name: &str) -> Option<&Value> {