diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index b3d9ec2f07..58c9c6dafc 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -49,29 +49,33 @@ impl Command for Mv { glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect); if sources.is_empty() { - return Err(ShellError::InternalError(format!( - "source \"{:?}\" does not exist", - source - ))); + return Err(ShellError::FileNotFound( + call.positional.first().unwrap().span, + )); } if (destination.exists() && !destination.is_dir() && sources.len() > 1) || (!destination.exists() && sources.len() > 1) { - return Err(ShellError::InternalError( - "can only move multiple sources if destination is a directory".to_string(), - )); + return Err(ShellError::MoveNotPossible { + source_message: "Can't move many files".to_string(), + source_span: call.positional[0].span, + destination_message: "into single file".to_string(), + destination_span: call.positional[1].span, + }); } let some_if_source_is_destination = sources .iter() .find(|f| matches!(f, Ok(f) if destination.starts_with(f))); if destination.exists() && destination.is_dir() && sources.len() == 1 { - if let Some(Ok(filename)) = some_if_source_is_destination { - return Err(ShellError::InternalError(format!( - "Not possible to move {:?} to itself", - filename.file_name().expect("Invalid file name") - ))); + if let Some(Ok(_filename)) = some_if_source_is_destination { + return Err(ShellError::MoveNotPossible { + source_message: "Can't move directory".to_string(), + source_span: call.positional[0].span, + destination_message: "into itself".to_string(), + destination_span: call.positional[1].span, + }); } } @@ -83,19 +87,21 @@ impl Command for Mv { } for entry in sources.into_iter().flatten() { - move_file(&entry, &destination)? + move_file(call, &entry, &destination)? } Ok(Value::Nothing { span: call.head }) } } -fn move_file(from: &PathBuf, to: &PathBuf) -> Result<(), ShellError> { +fn move_file(call: &Call, from: &PathBuf, to: &PathBuf) -> Result<(), ShellError> { if to.exists() && from.is_dir() && to.is_file() { - return Err(ShellError::InternalError(format!( - "Cannot rename {:?} to a file", - from.file_name().expect("Invalid directory name") - ))); + return Err(ShellError::MoveNotPossible { + source_message: "Can't move a directory".to_string(), + source_span: call.positional[0].span, + destination_message: "to a file".to_string(), + destination_span: call.positional[1].span, + }); } let destination_dir_exists = if to.is_dir() { @@ -105,37 +111,31 @@ fn move_file(from: &PathBuf, to: &PathBuf) -> Result<(), ShellError> { }; if !destination_dir_exists { - return Err(ShellError::InternalError(format!( - "{:?} does not exist", - to.file_name().expect("Invalid directory name") - ))); + return Err(ShellError::DirectoryNotFound(call.positional[1].span)); } let mut to = to.clone(); if to.is_dir() { let from_file_name = match from.file_name() { Some(name) => name, - None => { - return Err(ShellError::InternalError(format!( - "{:?} is not a valid entry", - from.file_name().expect("Invalid directory name") - ))) - } + None => return Err(ShellError::DirectoryNotFound(call.positional[1].span)), }; to.push(from_file_name); } - move_item(&from, &to) + move_item(call, &from, &to) } -fn move_item(from: &Path, to: &Path) -> Result<(), ShellError> { +fn move_item(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> { // We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy // and remove the old file/folder. This is necessary if we're moving across filesystems or devices. std::fs::rename(&from, &to).or_else(|_| { - Err(ShellError::InternalError(format!( - "Could not move {:?} to {:?}", - from, to, - ))) + Err(ShellError::MoveNotPossible { + source_message: "failed to move".to_string(), + source_span: call.positional[0].span, + destination_message: "into".to_string(), + destination_span: call.positional[1].span, + }) }) } diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 8307e99617..0bb59291e7 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -78,4 +78,23 @@ pub enum ShellError { #[error("Flag not found")] #[diagnostic(code(nu::shell::flag_not_found), url(docsrs))] FlagNotFound(String, #[label("{0} not found")] Span), + + #[error("File not found")] + #[diagnostic(code(nu::shell::file_not_found), url(docsrs))] + FileNotFound(#[label("file not found")] Span), + + #[error("Directory not found")] + #[diagnostic(code(nu::shell::directory_not_found), url(docsrs))] + DirectoryNotFound(#[label("directory not found")] Span), + + #[error("Move not possible")] + #[diagnostic(code(nu::shell::move_not_possible), url(docsrs))] + MoveNotPossible { + source_message: String, + #[label("{source_message}")] + source_span: Span, + destination_message: String, + #[label("{destination_message}")] + destination_span: Span, + }, }