From a0d4ae18ee4db64b0a8412638fe7b59878516e6a Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:04:23 -0600 Subject: [PATCH] better error message for "sum", "product", and "sum_of_squares" (#14711) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This PR tries to improve a few error messages. ### Before ![image](https://github.com/user-attachments/assets/58ab3ff6-baab-4075-8746-e83cb3acab14) ### After ![image](https://github.com/user-attachments/assets/9653776a-371b-4454-b092-3cc49f1329cd)   # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/math/reducers.rs | 8 ++++---- crates/nu-command/src/math/variance.rs | 6 +++--- crates/nu-command/tests/commands/math/sum.rs | 4 +--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/nu-command/src/math/reducers.rs b/crates/nu-command/src/math/reducers.rs index 86d0f8d7e4..6bda6fa632 100644 --- a/crates/nu-command/src/math/reducers.rs +++ b/crates/nu-command/src/math/reducers.rs @@ -111,8 +111,8 @@ pub fn sum(data: Vec, span: Span, head: Span) -> Result 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(), @@ -150,8 +150,8 @@ pub fn product(data: Vec, span: Span, head: Span) -> Result 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(), diff --git a/crates/nu-command/src/math/variance.rs b/crates/nu-command/src/math/variance.rs index 8422009230..91128e36cd 100644 --- a/crates/nu-command/src/math/variance.rs +++ b/crates/nu-command/src/math/variance.rs @@ -90,9 +90,9 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result { 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(), diff --git a/crates/nu-command/tests/commands/math/sum.rs b/crates/nu-command/tests/commands/math/sum.rs index 048a12e9e3..020ce552c9 100644 --- a/crates/nu-command/tests/commands/math/sum.rs +++ b/crates/nu-command/tests/commands/math/sum.rs @@ -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]