Skip to content

Commit

Permalink
Improve errors some more
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX committed Jan 6, 2023
1 parent 379e396 commit 62f2ab9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
10 changes: 5 additions & 5 deletions cpz/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ fn main() -> error_stack::Result<(), CliError> {
Error::Io { error, context } => Report::from(error)
.attach_printable(context)
.change_context(wrapper),
Error::PreserveRoot
| Error::Join
| Error::BadPath
| Error::AlreadyExists
| Error::Internal => Report::from(wrapper),
Error::AlreadyExists { file: _ } => {
Report::from(wrapper).attach_printable("Use --force to overwrite.")
}
Error::Join | Error::BadPath | Error::Internal => Report::from(wrapper),
Error::PreserveRoot => unreachable!(),
}
})
}
Expand Down
1 change: 1 addition & 0 deletions fuc_engine/api.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod fuc_engine
pub enum fuc_engine::Error
pub fuc_engine::Error::AlreadyExists
pub fuc_engine::Error::AlreadyExists::file: std::path::PathBuf
pub fuc_engine::Error::BadPath
pub fuc_engine::Error::Internal
pub fuc_engine::Error::Io
Expand Down
16 changes: 8 additions & 8 deletions fuc_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![feature(once_cell)]
#![allow(clippy::module_name_repetitions)]

use std::io;
use std::{io, path::PathBuf};

use thiserror::Error;

Expand All @@ -14,16 +14,16 @@ mod ops;

#[derive(Error, Debug)]
pub enum Error {
#[error("An IO error occurred.")]
#[error("An I/O error occurred")]
Io { error: io::Error, context: String },
#[error("An attempt was made to delete `/`.")]
#[error("An attempt was made to delete `/`")]
PreserveRoot,
#[error("Failed to join thread.")]
#[error("Failed to join thread")]
Join,
#[error("Invalid file path.")]
#[error("Invalid file path")]
BadPath,
#[error("File or directory already exists.")]
AlreadyExists,
#[error("An internal bug occurred, please report this.")]
#[error("File or directory already exists: {file:?}")]
AlreadyExists { file: PathBuf },
#[error("An internal bug occurred, please report this")]
Internal,
}
6 changes: 5 additions & 1 deletion fuc_engine/src/ops/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ fn schedule_copies<'a>(
for (from, to) in files {
if !force {
match to.metadata() {
Ok(_) => return Err(Error::AlreadyExists),
Ok(_) => {
return Err(Error::AlreadyExists {
file: to.into_owned(),
});
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
// Do nothing, this is good
}
Expand Down
9 changes: 4 additions & 5 deletions rmz/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ fn main() -> Result<(), CliError> {
Error::Io { error, context } => Report::from(error)
.attach_printable(context)
.change_context(wrapper),
Error::PreserveRoot
| Error::Join
| Error::BadPath
| Error::AlreadyExists
| Error::Internal => Report::from(wrapper),
Error::PreserveRoot | Error::Join | Error::BadPath | Error::Internal => {
Report::from(wrapper)
}
Error::AlreadyExists { file: _ } => unreachable!(),
}
})
}
Expand Down

0 comments on commit 62f2ab9

Please sign in to comment.