Outsource StorageBackendError building into helper function

This commit is contained in:
Matthias Beyer 2015-12-05 14:20:43 +01:00
parent 9f59478da0
commit e6a32eafc0

View file

@ -35,13 +35,8 @@ impl StorageBackend {
}) })
}).or_else(|e| { }).or_else(|e| {
debug!("Creating failed, constructing error instance"); debug!("Creating failed, constructing error instance");
let mut serr = StorageBackendError::new( Err(serr_build("create_dir_all()", "Could not create store directories",
"create_dir_all()", Some(storepath), Some(Box::new(e))))
"Could not create store directories",
Some(storepath)
);
serr.caused_by = Some(Box::new(e));
Err(serr)
}) })
} }
@ -56,13 +51,8 @@ impl StorageBackend {
}) })
.map_err(|e| { .map_err(|e| {
debug!("glob() returned error: {:?}", e); debug!("glob() returned error: {:?}", e);
let serr = StorageBackendError::new( serr_build("iter_ids()", "Cannot iter on file ids",
"iter_ids()", None, None)
"Cannot iter on file ids",
None);
// Why the hack is Error not implemented for glob::PatternError
// serr.caused_by = Some(Box::new(e));
serr
}) })
} }
@ -78,11 +68,8 @@ impl StorageBackend {
}) })
.map_err(|e| { .map_err(|e| {
debug!("StorageBackend::iter_ids() returned error = {:?}", e); debug!("StorageBackend::iter_ids() returned error = {:?}", e);
let mut serr = StorageBackendError::new("iter_files()", serr_build("iter_files()", "Cannot iter on files",
"Cannot iter on files", None, Some(Box::new(e)))
None);
serr.caused_by = Some(Box::new(e));
serr
}) })
} }
@ -107,23 +94,14 @@ impl StorageBackend {
file.write_all(&string.clone().into_bytes()) file.write_all(&string.clone().into_bytes())
.map_err(|ioerr| { .map_err(|ioerr| {
debug!("Could not write file"); debug!("Could not write file");
let mut err = StorageBackendError::new( serr_build("File::write_all()",
"File::write_all()", "Could not write out File contents",
"Could not write out File contents", None, Some(Box::new(ioerr)))
None
);
err.caused_by = Some(Box::new(ioerr));
err
}) })
}).map_err(|writeerr| { }).map_err(|writeerr| {
debug!("Could not create file at '{}'", path); debug!("Could not create file at '{}'", path);
let mut err = StorageBackendError::new( serr_build("File::create()", "Creating file on disk failed",
"File::create()", None, Some(Box::new(writeerr)))
"Creating file on disk failed",
None
);
err.caused_by = Some(Box::new(writeerr));
err
}).and(Ok(())) }).and(Ok(()))
} }
@ -147,23 +125,15 @@ impl StorageBackend {
file.write_all(&string.clone().into_bytes()) file.write_all(&string.clone().into_bytes())
.map_err(|ioerr| { .map_err(|ioerr| {
debug!("Could not write file"); debug!("Could not write file");
let mut err = StorageBackendError::new( serr_build("File::write()",
"File::write()", "Tried to write contents of this file, though operation did not succeed",
"Tried to write contents of this file, though operation did not succeed", Some(string), Some(Box::new(ioerr)))
Some(string)
);
err.caused_by = Some(Box::new(ioerr));
err
}) })
}).map_err(|writeerr| { }).map_err(|writeerr| {
debug!("Could not write file at '{}'", path); debug!("Could not write file at '{}'", path);
let mut err = StorageBackendError::new( serr_build("File::open()",
"File::open()", "Tried to update contents of this file, though file doesn't exist",
"Tried to update contents of this file, though file doesn't exist", None, Some(Box::new(writeerr)))
None
);
err.caused_by = Some(Box::new(writeerr));
err
}).and(Ok(())) }).and(Ok(()))
} }
@ -227,13 +197,8 @@ impl StorageBackend {
let fp = self.build_filepath(&file); let fp = self.build_filepath(&file);
remove_file(fp).map_err(|e| { remove_file(fp).map_err(|e| {
let mut serr = StorageBackendError::new( serr_build("remove_file()", "File removal failed",
"remove_file()", Some(format!("{}", file)), Some(Box::new(e)))
"File removal failed",
Some(format!("{}", file))
);
serr.caused_by = Some(Box::new(e));
serr
}) })
} }
@ -323,13 +288,9 @@ fn write_with_parser<'a, HP>(f: &File, p: &Parser<HP>) -> Result<String, Storage
{ {
p.write(f.contents()) p.write(f.contents())
.or_else(|err| { .or_else(|err| {
let mut serr = StorageBackendError::new( Err(serr_build("Parser::write()",
"Parser::write()", "Cannot translate internal representation of file contents into on-disk representation",
"Cannot translate internal representation of file contents into on-disk representation", None, Some(Box::new(err))))
None
);
serr.caused_by = Some(Box::new(err));
Err(serr)
}) })
} }
@ -339,3 +300,16 @@ fn globlist_to_file_id_vec(globlist: Paths) -> Vec<FileID> {
.collect::<Vec<FileID>>() .collect::<Vec<FileID>>()
} }
/*
* Helper to build a StorageBackendError object with cause, because one line is
* less than three lines
*/
fn serr_build(action: &'static str, desc: &'static str,
data: Option<String>, caused_by: Option<Box<Error>>)
-> StorageBackendError
{
let mut err = StorageBackendError::new(action, desc, data);
err.caused_by = caused_by;
err
}