forked from extern/nushell
Document and critically review ShellError
variants - Ep. 2 (#8326)
Continuation of #8229 # Description The `ShellError` enum at the moment is kind of messy. Many variants are basic tuple structs where you always have to reference the implementation with its macro invocation to know which field serves which purpose. Furthermore we have both variants that are kind of redundant or either overly broad to be useful for the user to match on or overly specific with few uses. So I set out to start fixing the lacking documentation and naming to make it feasible to critically review the individual usages and fix those. Furthermore we can decide to join or split up variants that don't seem to be fit for purpose. **Everyone:** Feel free to add review comments if you spot inconsistent use of `ShellError` variants. - Name fields of `SE::IncorrectValue` - Merge and name fields on `SE::TypeMismatch` - Name fields on `SE::UnsupportedOperator` - Name fields on `AssignmentRequires*` and fix doc - Name fields on `SE::UnknownOperator` - Name fields on `SE::MissingParameter` - Name fields on `SE::DelimiterError` - Name fields on `SE::IncompatibleParametersSingle` # User-Facing Changes (None now, end goal more explicit and consistent error messages) # Tests + Formatting (No additional tests needed so far)
This commit is contained in:
committed by
GitHub
parent
4ae1b1cc26
commit
f7b8f97873
@ -246,10 +246,10 @@ impl Command for Char {
|
||||
if call.has_flag("integer") {
|
||||
let args: Vec<i64> = call.rest(engine_state, stack, 0)?;
|
||||
if args.is_empty() {
|
||||
return Err(ShellError::MissingParameter(
|
||||
"missing at least one unicode character".into(),
|
||||
call_span,
|
||||
));
|
||||
return Err(ShellError::MissingParameter {
|
||||
param_name: "missing at least one unicode character".into(),
|
||||
span: call_span,
|
||||
});
|
||||
}
|
||||
let mut multi_byte = String::new();
|
||||
for (i, &arg) in args.iter().enumerate() {
|
||||
@ -263,10 +263,10 @@ impl Command for Char {
|
||||
} else if call.has_flag("unicode") {
|
||||
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||
if args.is_empty() {
|
||||
return Err(ShellError::MissingParameter(
|
||||
"missing at least one unicode character".into(),
|
||||
call_span,
|
||||
));
|
||||
return Err(ShellError::MissingParameter {
|
||||
param_name: "missing at least one unicode character".into(),
|
||||
span: call_span,
|
||||
});
|
||||
}
|
||||
let mut multi_byte = String::new();
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
@ -280,21 +280,22 @@ impl Command for Char {
|
||||
} else {
|
||||
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
|
||||
if args.is_empty() {
|
||||
return Err(ShellError::MissingParameter(
|
||||
"missing name of the character".into(),
|
||||
call_span,
|
||||
));
|
||||
return Err(ShellError::MissingParameter {
|
||||
param_name: "missing name of the character".into(),
|
||||
span: call_span,
|
||||
});
|
||||
}
|
||||
let special_character = str_to_character(&args[0]);
|
||||
if let Some(output) = special_character {
|
||||
Ok(Value::string(output, call_span).into_pipeline_data())
|
||||
} else {
|
||||
Err(ShellError::TypeMismatch(
|
||||
"error finding named character".into(),
|
||||
call.positional_nth(0)
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: "error finding named character".into(),
|
||||
span: call
|
||||
.positional_nth(0)
|
||||
.expect("Unexpected missing argument")
|
||||
.span,
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -306,10 +307,10 @@ fn integer_to_unicode_char(value: i64, t: &Span) -> Result<char, ShellError> {
|
||||
if let Some(ch) = decoded_char {
|
||||
Ok(ch)
|
||||
} else {
|
||||
Err(ShellError::TypeMismatch(
|
||||
"not a valid Unicode codepoint".into(),
|
||||
*t,
|
||||
))
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: "not a valid Unicode codepoint".into(),
|
||||
span: *t,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -321,10 +322,10 @@ fn string_to_unicode_char(s: &str, t: &Span) -> Result<char, ShellError> {
|
||||
if let Some(ch) = decoded_char {
|
||||
Ok(ch)
|
||||
} else {
|
||||
Err(ShellError::TypeMismatch(
|
||||
"error decoding Unicode character".into(),
|
||||
*t,
|
||||
))
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: "error decoding Unicode character".into(),
|
||||
span: *t,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,10 +187,10 @@ fn action(
|
||||
}
|
||||
}
|
||||
other => Value::Error {
|
||||
error: ShellError::TypeMismatch(
|
||||
format!("string or binary, not {}", other.get_type()),
|
||||
other.span().unwrap_or(command_span),
|
||||
),
|
||||
error: ShellError::TypeMismatch {
|
||||
err_message: format!("string or binary, not {}", other.get_type()),
|
||||
span: other.span().unwrap_or(command_span),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -157,10 +157,10 @@ fn extract_formatting_operations(
|
||||
}
|
||||
|
||||
if column_span_end < column_span_start {
|
||||
return Err(ShellError::DelimiterError(
|
||||
"there are unmatched curly braces".to_string(),
|
||||
error_span,
|
||||
));
|
||||
return Err(ShellError::DelimiterError {
|
||||
msg: "there are unmatched curly braces".to_string(),
|
||||
span: error_span,
|
||||
});
|
||||
}
|
||||
|
||||
if !column_name.is_empty() {
|
||||
@ -301,10 +301,10 @@ fn format_record(
|
||||
}
|
||||
}
|
||||
Some(err) => {
|
||||
return Err(ShellError::TypeMismatch(
|
||||
format!("expression is invalid, detail message: {err:?}"),
|
||||
*span,
|
||||
))
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: format!("expression is invalid, detail message: {err:?}"),
|
||||
span: *span,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,16 +26,16 @@ pub fn grapheme_flags(call: &Call) -> Result<bool, ShellError> {
|
||||
// Note that Nushell already prevents nonexistent flags from being used with commands,
|
||||
// so this function can be reused for both the --utf-8-bytes commands and the --code-points commands.
|
||||
if g_flag && call.has_flag("utf-8-bytes") {
|
||||
Err(ShellError::IncompatibleParametersSingle(
|
||||
"Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
call.head,
|
||||
))?
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
})?
|
||||
}
|
||||
if g_flag && call.has_flag("code-points") {
|
||||
Err(ShellError::IncompatibleParametersSingle(
|
||||
"Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
call.head,
|
||||
))?
|
||||
Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Incompatible flags: --grapheme-clusters (-g) and --utf-8-bytes (-b)".to_string(),
|
||||
span: call.head,
|
||||
})?
|
||||
}
|
||||
// Grapheme cluster usage is decided by the non-default -g flag
|
||||
Ok(g_flag)
|
||||
|
@ -285,10 +285,10 @@ fn build_regex(input: &str, span: Span) -> Result<String, ShellError> {
|
||||
column.push(c);
|
||||
|
||||
if loop_input.peek().is_none() {
|
||||
return Err(ShellError::DelimiterError(
|
||||
"Found opening `{` without an associated closing `}`".to_owned(),
|
||||
return Err(ShellError::DelimiterError {
|
||||
msg: "Found opening `{` without an associated closing `}`".to_owned(),
|
||||
span,
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -119,16 +119,16 @@ fn split_words(
|
||||
|
||||
if matches!(word_length, None) {
|
||||
if call.has_flag("grapheme-clusters") {
|
||||
return Err(ShellError::IncompatibleParametersSingle(
|
||||
"--grapheme-clusters (-g) requires --min-word-length (-l)".to_string(),
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "--grapheme-clusters (-g) requires --min-word-length (-l)".to_string(),
|
||||
span,
|
||||
));
|
||||
});
|
||||
}
|
||||
if call.has_flag("utf-8-bytes") {
|
||||
return Err(ShellError::IncompatibleParametersSingle(
|
||||
"--utf-8-bytes (-b) requires --min-word-length (-l)".to_string(),
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "--utf-8-bytes (-b) requires --min-word-length (-l)".to_string(),
|
||||
span,
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
let graphemes = grapheme_flags(call)?;
|
||||
|
@ -219,10 +219,10 @@ fn process_range(
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
if vals.len() > 2 {
|
||||
Err(ShellError::TypeMismatch(
|
||||
String::from("there shouldn't be more than two indexes"),
|
||||
head,
|
||||
))
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: String::from("there shouldn't be more than two indexes"),
|
||||
span: head,
|
||||
})
|
||||
} else {
|
||||
let idx: Vec<String> = vals
|
||||
.iter()
|
||||
@ -248,18 +248,15 @@ fn process_range(
|
||||
let end_index = r.1.parse::<i32>().unwrap_or(input_len as i32);
|
||||
|
||||
if start_index < 0 || start_index > end_index {
|
||||
return Err(ShellError::TypeMismatch(
|
||||
String::from("start index can't be negative or greater than end index"),
|
||||
head,
|
||||
));
|
||||
return Err(ShellError::TypeMismatch {
|
||||
err_message: String::from("start index can't be negative or greater than end index"),
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
|
||||
if end_index < 0 || end_index < start_index || end_index > input_len as i32 {
|
||||
return Err(ShellError::TypeMismatch(
|
||||
String::from(
|
||||
"end index can't be negative, smaller than start index or greater than input length"),
|
||||
head,
|
||||
));
|
||||
return Err(ShellError::TypeMismatch { err_message: String::from(
|
||||
"end index can't be negative, smaller than start index or greater than input length"), span: head });
|
||||
}
|
||||
Ok(IndexOfOptionalBounds(start_index, end_index))
|
||||
}
|
||||
|
@ -209,7 +209,10 @@ fn action(
|
||||
}
|
||||
}
|
||||
Err(e) => Value::Error {
|
||||
error: ShellError::IncorrectValue(format!("Regex error: {e}"), find.span),
|
||||
error: ShellError::IncorrectValue {
|
||||
msg: format!("Regex error: {e}"),
|
||||
span: find.span,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -155,10 +155,10 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||
match start.cmp(&end) {
|
||||
Ordering::Equal => Value::string("", head),
|
||||
Ordering::Greater => Value::Error {
|
||||
error: ShellError::TypeMismatch(
|
||||
"End must be greater than or equal to Start".to_string(),
|
||||
head,
|
||||
),
|
||||
error: ShellError::TypeMismatch {
|
||||
err_message: "End must be greater than or equal to Start".to_string(),
|
||||
span: head,
|
||||
},
|
||||
},
|
||||
Ordering::Less => Value::String {
|
||||
val: {
|
||||
@ -220,10 +220,10 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
if vals.len() > 2 {
|
||||
Err(ShellError::TypeMismatch(
|
||||
"More than two indices given".to_string(),
|
||||
head,
|
||||
))
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: "More than two indices given".to_string(),
|
||||
span: head,
|
||||
})
|
||||
} else {
|
||||
let idx: Vec<String> = vals
|
||||
.iter()
|
||||
@ -231,11 +231,12 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||
match v {
|
||||
Value::Int { val, .. } => Ok(val.to_string()),
|
||||
Value::String { val, .. } => Ok(val.to_string()),
|
||||
_ => Err(ShellError::TypeMismatch(
|
||||
"could not perform substring. Expecting a string or int"
|
||||
.to_string(),
|
||||
head,
|
||||
)),
|
||||
_ => Err(ShellError::TypeMismatch {
|
||||
err_message:
|
||||
"could not perform substring. Expecting a string or int"
|
||||
.to_string(),
|
||||
span: head,
|
||||
}),
|
||||
}
|
||||
.unwrap_or_else(|_| String::from(""))
|
||||
})
|
||||
@ -243,14 +244,16 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||
|
||||
let start = idx
|
||||
.get(0)
|
||||
.ok_or_else(|| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
.ok_or_else(|| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.to_string();
|
||||
let end = idx
|
||||
.get(1)
|
||||
.ok_or_else(|| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
.ok_or_else(|| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.to_string();
|
||||
Ok(SubstringText(start, end))
|
||||
@ -261,35 +264,39 @@ fn process_arguments(range: &Value, head: Span) -> Result<(isize, isize), ShellE
|
||||
|
||||
let start = idx
|
||||
.first()
|
||||
.ok_or_else(|| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
.ok_or_else(|| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.to_string();
|
||||
let end = idx
|
||||
.get(1)
|
||||
.ok_or_else(|| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
.ok_or_else(|| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.to_string();
|
||||
|
||||
Ok(SubstringText(start, end))
|
||||
}
|
||||
_ => Err(ShellError::TypeMismatch(
|
||||
"could not perform substring".to_string(),
|
||||
head,
|
||||
)),
|
||||
_ => Err(ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
}),
|
||||
}?;
|
||||
let start = match &search {
|
||||
SubstringText(start, _) if start.is_empty() || start == "_" => 0,
|
||||
SubstringText(start, _) => start.trim().parse().map_err(|_| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
SubstringText(start, _) => start.trim().parse().map_err(|_| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?,
|
||||
};
|
||||
|
||||
let end = match &search {
|
||||
SubstringText(_, end) if end.is_empty() || end == "_" => isize::max_value(),
|
||||
SubstringText(_, end) => end.trim().parse().map_err(|_| {
|
||||
ShellError::TypeMismatch("could not perform substring".to_string(), head)
|
||||
SubstringText(_, end) => end.trim().parse().map_err(|_| ShellError::TypeMismatch {
|
||||
err_message: "could not perform substring".to_string(),
|
||||
span: head,
|
||||
})?,
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user