forked from extern/nushell
Standardise the use of ShellError::UnsupportedInput and ShellError::TypeMismatch and add spans to every instance of the former (#7217)
# Description * I was dismayed to discover recently that UnsupportedInput and TypeMismatch are used *extremely* inconsistently across the codebase. UnsupportedInput is sometimes used for input type-checks (as per the name!!), but *also* used for argument type-checks. TypeMismatch is also used for both. I thus devised the following standard: input type-checking *only* uses UnsupportedInput, and argument type-checking *only* uses TypeMismatch. Moreover, to differentiate them, UnsupportedInput now has *two* error arrows (spans), one pointing at the command and the other at the input origin, while TypeMismatch only has the one (because the command should always be nearby) * In order to apply that standard, a very large number of UnsupportedInput uses were changed so that the input's span could be retrieved and delivered to it. * Additionally, I noticed many places where **errors are not propagated correctly**: there are lots of `match` sites which take a Value::Error, then throw it away and replace it with a new Value::Error with less/misleading information (such as reporting the error as an "incorrect type"). I believe that the earliest errors are the most important, and should always be propagated where possible. * Also, to standardise one broad subset of UnsupportedInput error messages, who all used slightly different wordings of "expected `<type>`, got `<type>`", I created OnlySupportsThisInputType as a variant of it. * Finally, a bunch of error sites that had "repeated spans" - i.e. where an error expected two spans, but `call.head` was given for both - were fixed to use different spans. # Example BEFORE ``` 〉20b | str starts-with 'a' Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #31:1:1] 1 │ 20b | str starts-with 'a' · ┬ · ╰── Input's type is filesize. This command only works with strings. ╰──── 〉'a' | math cos Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #33:1:1] 1 │ 'a' | math cos · ─┬─ · ╰── Only numerical values are supported, input type: String ╰──── 〉0x[12] | encode utf8 Error: nu:🐚:unsupported_input (link) × Unsupported input ╭─[entry #38:1:1] 1 │ 0x[12] | encode utf8 · ───┬── · ╰── non-string input ╰──── ``` AFTER ``` 〉20b | str starts-with 'a' Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #1:1:1] 1 │ 20b | str starts-with 'a' · ┬ ───────┬─────── · │ ╰── only string input data is supported · ╰── input type: filesize ╰──── 〉'a' | math cos Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #2:1:1] 1 │ 'a' | math cos · ─┬─ ────┬─── · │ ╰── only numeric input data is supported · ╰── input type: string ╰──── 〉0x[12] | encode utf8 Error: nu:🐚:pipeline_mismatch (link) × Pipeline mismatch. ╭─[entry #3:1:1] 1 │ 0x[12] | encode utf8 · ───┬── ───┬── · │ ╰── only string input data is supported · ╰── input type: binary ╰──── ``` # User-Facing Changes Various error messages suddenly make more sense (i.e. have two arrows instead of one). # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use std::path::Path;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Type, Value};
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
|
||||
Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
|
||||
@@ -62,6 +65,10 @@ impl Command for SubCommand {
|
||||
replace: call.get_flag(engine_state, stack, "replace")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&get_basename, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -1,7 +1,10 @@
|
||||
use std::path::Path;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{engine::Command, Example, Signature, Span, Spanned, SyntaxShape, Type, Value};
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
|
||||
Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
|
||||
@@ -66,6 +69,10 @@ impl Command for SubCommand {
|
||||
num_levels: call.get_flag(engine_state, stack, "num-levels")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&get_dirname, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -2,7 +2,9 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_engine::{current_dir, CallExt};
|
||||
use nu_path::expand_path_with;
|
||||
use nu_protocol::{engine::Command, Example, Signature, Span, SyntaxShape, Type, Value};
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
|
||||
@@ -57,6 +59,10 @@ If you need to distinguish dirs and files, please use `path type`."#
|
||||
columns: call.get_flag(engine_state, stack, "columns")?,
|
||||
pwd: current_dir(engine_state, stack)?,
|
||||
};
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&exists, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -4,7 +4,7 @@ use nu_engine::env::current_dir_str;
|
||||
use nu_engine::CallExt;
|
||||
use nu_path::{canonicalize_with, expand_path_with};
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@@ -65,7 +65,10 @@ impl Command for SubCommand {
|
||||
cwd: current_dir_str(engine_state, stack)?,
|
||||
not_follow_symlink: call.has_flag("no-symlink"),
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&expand, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -78,9 +78,14 @@ the output of 'path parse' and 'path split' subcommands."#
|
||||
handle_value(input.into_value(head), &args, head),
|
||||
metadata,
|
||||
)),
|
||||
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty(head)),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Input data is not supported by this command.".to_string(),
|
||||
"Input value cannot be joined".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
input
|
||||
.span()
|
||||
.expect("non-Empty non-ListStream PipelineData had no span"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
@@ -156,35 +161,35 @@ the output of 'path parse' and 'path split' subcommands."#
|
||||
|
||||
fn handle_value(v: Value, args: &Arguments, head: Span) -> Value {
|
||||
match v {
|
||||
Value::String { ref val, span } => join_single(Path::new(val), span, args),
|
||||
Value::Record { cols, vals, span } => join_record(&cols, &vals, span, args),
|
||||
Value::List { vals, span } => join_list(&vals, span, head, args),
|
||||
Value::String { ref val, .. } => join_single(Path::new(val), head, args),
|
||||
Value::Record { cols, vals, span } => join_record(&cols, &vals, head, span, args),
|
||||
Value::List { vals, span } => join_list(&vals, head, span, args),
|
||||
|
||||
_ => super::handle_invalid_values(v, head),
|
||||
}
|
||||
}
|
||||
|
||||
fn join_single(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||
fn join_single(path: &Path, head: Span, args: &Arguments) -> Value {
|
||||
let mut result = path.to_path_buf();
|
||||
for path_to_append in &args.append {
|
||||
result.push(&path_to_append.item)
|
||||
}
|
||||
|
||||
Value::string(result.to_string_lossy(), span)
|
||||
Value::string(result.to_string_lossy(), head)
|
||||
}
|
||||
|
||||
fn join_list(parts: &[Value], span: Span, head: Span, args: &Arguments) -> Value {
|
||||
fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value {
|
||||
let path: Result<PathBuf, ShellError> = parts.iter().map(Value::as_string).collect();
|
||||
|
||||
match path {
|
||||
Ok(ref path) => join_single(path, span, args),
|
||||
Ok(ref path) => join_single(path, head, args),
|
||||
Err(_) => {
|
||||
let records: Result<Vec<_>, ShellError> = parts.iter().map(Value::as_record).collect();
|
||||
match records {
|
||||
Ok(vals) => {
|
||||
let vals = vals
|
||||
.iter()
|
||||
.map(|(k, v)| join_record(k, v, span, args))
|
||||
.map(|(k, v)| join_record(k, v, head, span, args))
|
||||
.collect();
|
||||
|
||||
Value::List { vals, span }
|
||||
@@ -197,7 +202,7 @@ fn join_list(parts: &[Value], span: Span, head: Span, args: &Arguments) -> Value
|
||||
}
|
||||
}
|
||||
|
||||
fn join_record(cols: &[String], vals: &[Value], span: Span, args: &Arguments) -> Value {
|
||||
fn join_record(cols: &[String], vals: &[Value], head: Span, span: Span, args: &Arguments) -> Value {
|
||||
if args.columns.is_some() {
|
||||
super::operate(
|
||||
&join_single,
|
||||
@@ -210,22 +215,31 @@ fn join_record(cols: &[String], vals: &[Value], span: Span, args: &Arguments) ->
|
||||
span,
|
||||
)
|
||||
} else {
|
||||
match merge_record(cols, vals, span) {
|
||||
Ok(p) => join_single(p.as_path(), span, args),
|
||||
match merge_record(cols, vals, head, span) {
|
||||
Ok(p) => join_single(p.as_path(), head, args),
|
||||
Err(error) => Value::Error { error },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_record(cols: &[String], vals: &[Value], span: Span) -> Result<PathBuf, ShellError> {
|
||||
fn merge_record(
|
||||
cols: &[String],
|
||||
vals: &[Value],
|
||||
head: Span,
|
||||
span: Span,
|
||||
) -> Result<PathBuf, ShellError> {
|
||||
for key in cols {
|
||||
if !super::ALLOWED_COLUMNS.contains(&key.as_str()) {
|
||||
let allowed_cols = super::ALLOWED_COLUMNS.join(", ");
|
||||
let msg = format!(
|
||||
"Column '{}' is not valid for a structured path. Allowed columns are: {}",
|
||||
key, allowed_cols
|
||||
);
|
||||
return Err(ShellError::UnsupportedInput(msg, span));
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
format!(
|
||||
"Column '{}' is not valid for a structured path. Allowed columns on this platform are: {}",
|
||||
key, allowed_cols
|
||||
),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
span
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -50,7 +50,9 @@ where
|
||||
return Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
String::from("when the input is a table, you must specify the columns"),
|
||||
"value originates from here".into(),
|
||||
name,
|
||||
span,
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -91,9 +93,11 @@ fn err_from_value(rest: &Value, name: Span) -> ShellError {
|
||||
match rest.span() {
|
||||
Ok(span) => {
|
||||
if rest.is_nothing() {
|
||||
ShellError::UnsupportedInput(
|
||||
"Input type is nothing, expected: string, row or list".into(),
|
||||
ShellError::OnlySupportsThisInputType(
|
||||
"string, record or list".into(),
|
||||
"nothing".into(),
|
||||
name,
|
||||
span,
|
||||
)
|
||||
} else {
|
||||
ShellError::PipelineMismatch("string, row or list".into(), name, span)
|
||||
|
@@ -3,7 +3,8 @@ use std::path::Path;
|
||||
use indexmap::IndexMap;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
|
||||
Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@@ -66,6 +67,10 @@ On Windows, an extra 'prefix' column is added."#
|
||||
extension: call.get_flag(engine_state, stack, "extension")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&parse, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -3,7 +3,8 @@ use std::path::Path;
|
||||
use nu_engine::CallExt;
|
||||
use nu_path::expand_to_real_path;
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
|
||||
Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@@ -66,6 +67,10 @@ path."#
|
||||
columns: call.get_flag(engine_state, stack, "columns")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&relative_to, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -2,7 +2,7 @@ use std::path::{Component, Path};
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@@ -52,6 +52,10 @@ impl Command for SubCommand {
|
||||
columns: call.get_flag(engine_state, stack, "columns")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&split, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
@@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
engine::Command, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@@ -57,6 +57,10 @@ If nothing is found, an empty string will be returned."#
|
||||
columns: call.get_flag(engine_state, stack, "columns")?,
|
||||
};
|
||||
|
||||
// This doesn't match explicit nulls
|
||||
if matches!(input, PipelineData::Empty) {
|
||||
return Err(ShellError::PipelineEmpty(head));
|
||||
}
|
||||
input.map(
|
||||
move |value| super::operate(&r#type, &args, value, head),
|
||||
engine_state.ctrlc.clone(),
|
||||
|
Reference in New Issue
Block a user