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",