Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error message for "sum", "product", and "sum_of_squares" #14711

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/nu-command/src/math/reducers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
Value::Error { error, .. } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the sum of a value that cannot be summed"
.to_string(),
msg: format!("Attempted to compute the sum of a value '{}' that cannot be summed with a type of `{}`.",
other.coerce_string()?, other.get_type()),
input: "value originates from here".into(),
msg_span: head,
input_span: other.span(),
Expand Down Expand Up @@ -150,8 +150,8 @@ pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellE
Value::Error { error, .. } => return Err(*error.clone()),
other => {
return Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the product of a value that cannot be multiplied"
.to_string(),
msg: format!("Attempted to compute the product of a value '{}' that cannot be multiplied with a type of `{}`.",
other.coerce_string()?, other.get_type()),
input: "value originates from here".into(),
msg_span: head,
input_span: other.span(),
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/math/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result<Value, ShellError> {
let v = match &value {
Value::Int { .. } | Value::Float { .. } => Ok(value),
Value::Error { error, .. } => Err(*error.clone()),
_ => Err(ShellError::UnsupportedInput {
msg: "Attempted to compute the sum of squares of a non-int, non-float value"
.to_string(),
other => Err(ShellError::UnsupportedInput {
msg: format!("Attempted to compute the sum of squares of a non-int, non-float value '{}' with a type of `{}`.",
other.coerce_string()?, other.get_type()),
input: "value originates from here".into(),
msg_span: span,
input_span: value.span(),
Expand Down
4 changes: 1 addition & 3 deletions crates/nu-command/tests/commands/math/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ fn sum_of_a_row_containing_a_table_is_an_error() {
cwd: "tests/fixtures/formats/",
"open sample-sys-output.json | math sum"
);
assert!(actual
.err
.contains("Attempted to compute the sum of a value that cannot be summed"));
assert!(actual.err.contains("can't convert record"));
}

#[test]
Expand Down
Loading