More precise ErrorKind::NotFound errors (#15149)

In this PR, the two new variants for `ErrorKind`, `FileNotFound`
and `DirectoryNotFound` with a nice `not_found_as` method for the
`ErrorKind` to easily specify the `NotFound` errors. I also updated some
places where I could of think of with these new variants and the message
for `NotFound` is no longer "Entity not found" but "Not found" to be
less strange.

closes #15142
closes #15055
This commit is contained in:
Piepmatz
2025-02-22 17:42:44 +01:00
committed by GitHub
parent 1d44843970
commit bda3245725
13 changed files with 139 additions and 36 deletions

View File

@ -1,7 +1,5 @@
use chrono::Local;
use nu_engine::command_prelude::*;
use nu_protocol::shell_error::io::IoError;
use nu_utils::{get_scaffold_config, get_scaffold_env};
use std::{io::Write, path::PathBuf};
@ -61,7 +59,7 @@ impl Command for ConfigReset {
));
if let Err(err) = std::fs::rename(nu_config.clone(), &backup_path) {
return Err(ShellError::Io(IoError::new_with_additional_context(
err.kind(),
err.kind().not_found_as(NotFound::Directory),
span,
PathBuf::from(backup_path),
"config.nu could not be backed up",
@ -71,7 +69,7 @@ impl Command for ConfigReset {
if let Ok(mut file) = std::fs::File::create(&nu_config) {
if let Err(err) = writeln!(&mut file, "{config_file}") {
return Err(ShellError::Io(IoError::new_with_additional_context(
err.kind(),
err.kind().not_found_as(NotFound::File),
span,
PathBuf::from(nu_config),
"config.nu could not be written to",
@ -88,7 +86,7 @@ impl Command for ConfigReset {
backup_path.push(format!("oldenv-{}.nu", Local::now().format("%F-%H-%M-%S"),));
if let Err(err) = std::fs::rename(env_config.clone(), &backup_path) {
return Err(ShellError::Io(IoError::new_with_additional_context(
err.kind(),
err.kind().not_found_as(NotFound::Directory),
span,
PathBuf::from(backup_path),
"env.nu could not be backed up",
@ -98,7 +96,7 @@ impl Command for ConfigReset {
if let Ok(mut file) = std::fs::File::create(&env_config) {
if let Err(err) = writeln!(&mut file, "{config_file}") {
return Err(ShellError::Io(IoError::new_with_additional_context(
err.kind(),
err.kind().not_found_as(NotFound::File),
span,
PathBuf::from(env_config),
"env.nu could not be written to",

View File

@ -88,7 +88,7 @@ impl Command for Cd {
path
} else {
return Err(shell_error::io::IoError::new(
std::io::ErrorKind::NotFound,
ErrorKind::DirectoryNotFound,
v.span,
PathBuf::from(path_no_whitespace),
)
@ -98,7 +98,7 @@ impl Command for Cd {
let path = nu_path::expand_path_with(path_no_whitespace, &cwd, true);
if !path.exists() {
return Err(shell_error::io::IoError::new(
std::io::ErrorKind::NotFound,
ErrorKind::DirectoryNotFound,
v.span,
PathBuf::from(path_no_whitespace),
)

View File

@ -98,6 +98,7 @@ impl Command for Open {
for path in nu_engine::glob_from(&path, &cwd, call_span, None)
.map_err(|err| match err {
ShellError::Io(mut err) => {
err.kind = err.kind.not_found_as(NotFound::File);
err.span = arg_span;
err.into()
}

View File

@ -55,8 +55,13 @@ impl Command for Source {
let cwd = engine_state.cwd_as_string(Some(stack))?;
let pb = std::path::PathBuf::from(block_id_name);
let parent = pb.parent().unwrap_or(std::path::Path::new(""));
let file_path = canonicalize_with(pb.as_path(), cwd)
.map_err(|err| IoError::new(err.kind(), call.head, pb.clone()))?;
let file_path = canonicalize_with(pb.as_path(), cwd).map_err(|err| {
IoError::new(
err.kind().not_found_as(NotFound::File),
call.head,
pb.clone(),
)
})?;
// Note: We intentionally left out PROCESS_PATH since it's supposed to
// to work like argv[0] in C, which is the name of the program being executed.

View File

@ -98,7 +98,7 @@ impl Command for NuCheck {
Ok(Some(path)) => path,
Ok(None) => {
return Err(ShellError::Io(IoError::new(
std::io::ErrorKind::NotFound,
ErrorKind::FileNotFound,
path_span,
PathBuf::from(path_str.item),
)))
@ -255,7 +255,7 @@ fn parse_file_script(
match std::fs::read(path) {
Ok(contents) => parse_script(working_set, Some(&filename), &contents, is_debug, call_head),
Err(err) => Err(ShellError::Io(IoError::new(
err.kind(),
err.kind().not_found_as(NotFound::File),
path_span,
PathBuf::from(path),
))),