Convert FileNotFoundCustom to named fields (#11123)

# Description

Part of #10700

# User-Facing Changes

None

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A
This commit is contained in:
Eric Hodel 2023-11-21 15:30:21 -08:00 committed by GitHub
parent 5ad7b8f029
commit e36f69bf3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 39 deletions

View File

@ -35,10 +35,10 @@ pub fn evaluate_file(
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!("Could not access file '{}': {:?}", path, e.to_string()),
Span::unknown(),
),
&ShellError::FileNotFoundCustom {
msg: format!("Could not access file '{}': {:?}", path, e.to_string()),
span: Span::unknown(),
},
);
std::process::exit(1);
});
@ -64,14 +64,14 @@ pub fn evaluate_file(
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!(
&ShellError::FileNotFoundCustom {
msg: format!(
"Could not read file '{}': {:?}",
file_path_str,
e.to_string()
),
Span::unknown(),
),
span: Span::unknown(),
},
);
std::process::exit(1);
});
@ -82,10 +82,10 @@ pub fn evaluate_file(
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!("The file path '{file_path_str}' does not have a parent"),
Span::unknown(),
),
&ShellError::FileNotFoundCustom {
msg: format!("The file path '{file_path_str}' does not have a parent"),
span: Span::unknown(),
},
);
std::process::exit(1);
});

View File

@ -121,15 +121,15 @@ fn command(
"json" => from_json(engine_state, stack, call),
"jsonl" => from_jsonl(engine_state, stack, call),
"avro" => from_avro(engine_state, stack, call),
_ => Err(ShellError::FileNotFoundCustom(
format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
blamed,
)),
_ => Err(ShellError::FileNotFoundCustom {
msg: format!("{msg}. Supported values: csv, tsv, parquet, ipc, arrow, json"),
span: blamed,
}),
},
None => Err(ShellError::FileNotFoundCustom(
"File without extension".into(),
file.span,
)),
None => Err(ShellError::FileNotFoundCustom {
msg: "File without extension".into(),
span: file.span,
}),
}
.map(|value| PipelineData::Value(value, None))
}

View File

@ -72,18 +72,18 @@ impl Command for ConfigReset {
Local::now().format("%F-%H-%M-%S"),
));
if std::fs::rename(nu_config.clone(), backup_path).is_err() {
return Err(ShellError::FileNotFoundCustom(
"config.nu could not be backed up".into(),
return Err(ShellError::FileNotFoundCustom {
msg: "config.nu could not be backed up".into(),
span,
));
});
}
}
if let Ok(mut file) = std::fs::File::create(nu_config) {
if writeln!(&mut file, "{config_file}").is_err() {
return Err(ShellError::FileNotFoundCustom(
"config.nu could not be written to".into(),
return Err(ShellError::FileNotFoundCustom {
msg: "config.nu could not be written to".into(),
span,
));
});
}
}
}
@ -95,18 +95,18 @@ impl Command for ConfigReset {
let mut backup_path = config_path.clone();
backup_path.push(format!("oldenv-{}.nu", Local::now().format("%F-%H-%M-%S"),));
if std::fs::rename(env_config.clone(), backup_path).is_err() {
return Err(ShellError::FileNotFoundCustom(
"env.nu could not be backed up".into(),
return Err(ShellError::FileNotFoundCustom {
msg: "env.nu could not be backed up".into(),
span,
));
});
}
}
if let Ok(mut file) = std::fs::File::create(env_config) {
if writeln!(&mut file, "{config_file}").is_err() {
return Err(ShellError::FileNotFoundCustom(
"env.nu could not be written to".into(),
return Err(ShellError::FileNotFoundCustom {
msg: "env.nu could not be written to".into(),
span,
));
});
}
}
}

View File

@ -563,9 +563,15 @@ fn convert_io_error(error: std::io::Error, src: PathBuf, dst: PathBuf, span: Spa
let shell_error = match error.kind() {
ErrorKind::NotFound => {
if std::path::Path::new(&dst).exists() {
ShellError::FileNotFoundCustom(message_src, span)
ShellError::FileNotFoundCustom {
msg: message_src,
span,
}
} else {
ShellError::FileNotFoundCustom(message_dst, span)
ShellError::FileNotFoundCustom {
msg: message_dst,
span,
}
}
}
ErrorKind::PermissionDenied => match std::fs::metadata(&dst) {

View File

@ -725,7 +725,11 @@ pub enum ShellError {
/// Does the file in the error message exist? Is it readable and accessible? Is the casing right?
#[error("File not found")]
#[diagnostic(code(nu::shell::file_not_found))]
FileNotFoundCustom(String, #[label("{0}")] Span),
FileNotFoundCustom {
msg: String,
#[label("{msg}")]
span: Span,
},
/// A plugin failed to load.
///

View File

@ -59,10 +59,10 @@ fn read_in_file<'a>(
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!("Could not read file '{}': {:?}", file_path, e.to_string()),
Span::unknown(),
),
&ShellError::FileNotFoundCustom {
msg: format!("Could not read file '{}': {:?}", file_path, e.to_string()),
span: Span::unknown(),
},
);
std::process::exit(1);
});