Skip to content

Commit

Permalink
update error kind and error display impl
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Oct 30, 2024
1 parent 5d41598 commit 8e8abe1
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 131 deletions.
14 changes: 7 additions & 7 deletions crates/backend/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl WriteBackend for LocalBackend {
trace!("creating repo at {:?}", self.path);
fs::create_dir_all(&self.path).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to create the directory. Please check the path and try again.",
err,
)
Expand All @@ -416,7 +416,7 @@ impl WriteBackend for LocalBackend {
let path = self.path.join(tpe.dirname());
fs::create_dir_all(path.clone()).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to create the directory. Please check the path and try again.",
err,
)
Expand All @@ -428,7 +428,7 @@ impl WriteBackend for LocalBackend {
let path = self.path.join("data").join(hex::encode([i]));
fs::create_dir_all(path.clone()).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to create the directory. Please check the path and try again.",
err,
)
Expand Down Expand Up @@ -471,7 +471,7 @@ impl WriteBackend for LocalBackend {
.open(&filename)
.map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to open the file. Please check the file and try again.",
err,
)
Expand All @@ -485,7 +485,7 @@ impl WriteBackend for LocalBackend {
})?)
.map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to set the length of the file. Please check the file and try again.",
err,
)
Expand All @@ -494,7 +494,7 @@ impl WriteBackend for LocalBackend {

file.write_all(&buf).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to write to the buffer. Please check the file and try again.",
err,
)
Expand All @@ -503,7 +503,7 @@ impl WriteBackend for LocalBackend {

file.sync_all().map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to sync OS Metadata to disk. Please check the file and try again.",
err,
)
Expand Down
4 changes: 2 additions & 2 deletions crates/backend/src/rclone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl RcloneBackend {
.read_line(&mut line)
.map_err(|err|
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Experienced an error while reading rclone output. Please check if rclone is installed and working correctly.",
err
)
Expand All @@ -269,7 +269,7 @@ impl RcloneBackend {
if use_password {
if !rest_url.starts_with("http://") {
return Err(RusticError::new(
ErrorKind::Io,
ErrorKind::InputOutput,
"Please make sure, the URL starts with 'http://'!",
)
.attach_context("url", rest_url));
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/archiver/file_archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a, BE: DecryptWriteBackend, I: ReadGlobalIndex> FileArchiver<'a, BE, I> {
.open()
.map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to open ReadSourceOpen",
err,
)
Expand Down
46 changes: 28 additions & 18 deletions crates/core/src/backend/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,30 @@ impl Cache {
};

fs::create_dir_all(&path).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to create cache directory", err)
.attach_context("path", path.display().to_string())
.attach_context("id", id.to_string())
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to create cache directory",
err,
)
.attach_context("path", path.display().to_string())
.attach_context("id", id.to_string())
})?;

cachedir::ensure_tag(&path).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to ensure cache directory tag", err)
.attach_context("path", path.display().to_string())
.attach_context("id", id.to_string())
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to ensure cache directory tag",
err,
)
.attach_context("path", path.display().to_string())
.attach_context("id", id.to_string())
})?;

path.push(id.to_hex());

fs::create_dir_all(&path).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to create cache directory with id",
err,
)
Expand Down Expand Up @@ -400,7 +408,7 @@ impl Cache {
}
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to read full data of file",
err,
)
Expand Down Expand Up @@ -440,18 +448,20 @@ impl Cache {
Ok(file) => file,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => {
return Err(
RusticError::with_source(ErrorKind::Io, "Failed to open file", err)
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string()),
return Err(RusticError::with_source(
ErrorKind::InputOutput,
"Failed to open file",
err,
)
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string()))
}
};

_ = file
.seek(SeekFrom::Start(u64::from(offset)))
.map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to seek in file", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to seek in file", err)
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string())
.attach_context("offset", offset.to_string())
Expand All @@ -460,7 +470,7 @@ impl Cache {
let mut vec = vec![0; length as usize];

file.read_exact(&mut vec).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to read from file", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to read from file", err)
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string())
.attach_context("offset", offset.to_string())
Expand Down Expand Up @@ -489,7 +499,7 @@ impl Cache {
let dir = self.dir(tpe, id);

fs::create_dir_all(&dir).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to create directories", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to create directories", err)
.attach_context("path", dir.display().to_string())
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string())
Expand All @@ -503,12 +513,12 @@ impl Cache {
.write(true)
.open(&filename)
.map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to open file", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to open file", err)
.attach_context("path", filename.display().to_string())
})?;

file.write_all(buf).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to write to buffer", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to write to buffer", err)
.attach_context("path", filename.display().to_string())
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string())
Expand All @@ -531,7 +541,7 @@ impl Cache {
trace!("cache writing tpe: {:?}, id: {}", &tpe, &id);
let filename = self.path(tpe, id);
fs::remove_file(&filename).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to remove file", err)
RusticError::with_source(ErrorKind::InputOutput, "Failed to remove file", err)
.attach_context("path", filename.display().to_string())
.attach_context("tpe", tpe.to_string())
.attach_context("id", id.to_string())
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/backend/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl ReadSourceOpen for OpenFile {
let path = self.0;
File::open(&path).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to open file. Please make sure the file exists and is accessible.",
err,
)
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/backend/local_destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl LocalDestination {
if let Some(path) = path.parent() {
fs::create_dir_all(path).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"The directory could not be created.",
err,
)
Expand All @@ -163,7 +163,7 @@ impl LocalDestination {
} else {
fs::create_dir_all(&path).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"The directory could not be created.",
err,
)
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/chunker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<R: Read + Send> Iterator for ChunkIter<R> {
Ok(size) => size,
Err(err) => {
return Some(Err(RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to read from reader in iterator",
err,
)));
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<R: Read + Send> Iterator for ChunkIter<R> {
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => {
return Some(Err(RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to read from reader in iterator",
err,
)));
Expand Down
6 changes: 5 additions & 1 deletion crates/core/src/commands/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ pub(crate) fn dump<P, S: IndexedFull>(
for id in node.content.as_ref().unwrap() {
let data = repo.get_blob_cached(&BlobId::from(**id), BlobType::Data)?;
w.write_all(&data).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to write data to writer.", err)
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to write data to writer.",
err,
)
})?;
}
Ok(())
Expand Down
6 changes: 5 additions & 1 deletion crates/core/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ pub(crate) fn add_key_to_repo<P, S>(
let keyfile = KeyFile::generate(key, &pass, ko.hostname, ko.username, ko.with_created)?;

let data = serde_json::to_vec(&keyfile).map_err(|err| {
RusticError::with_source(ErrorKind::Io, "Failed to serialize keyfile to JSON.", err)
RusticError::with_source(
ErrorKind::InputOutput,
"Failed to serialize keyfile to JSON.",
err,
)
})?;

let id = KeyId::from(hash(&data));
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub(crate) fn collect_and_prepare<P: ProgressBars, S: IndexedFull>(
dest.create_dir(path)
.map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to create the directory. Please check the path and try again.",
err
)
Expand Down Expand Up @@ -453,7 +453,7 @@ fn restore_contents<P: ProgressBars, S: Open>(
let path = &filenames[i];
dest.set_length(path, *size).map_err(|err| {
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to set the length of the file. Please check the path and try again.",
err,
)
Expand Down Expand Up @@ -678,7 +678,7 @@ impl RestorePlan {
.transpose()
.map_err(|err|
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to get the metadata of the file. Please check the path and try again.",
err
)
Expand All @@ -699,7 +699,7 @@ impl RestorePlan {
.transpose()
.map_err(|err|
RusticError::with_source(
ErrorKind::Io,
ErrorKind::InputOutput,
"Failed to get the metadata of the file. Please check the path and try again.",
err
)
Expand Down
Loading

0 comments on commit 8e8abe1

Please sign in to comment.