forked from extern/nushell
Improve CantFindColumn and ColumnAlreadyExists errors (#7164)
* Improve CantFindColumn and ColumnAlreadyExists errors * Update tests
This commit is contained in:
@ -250,6 +250,7 @@ pub fn group(
|
||||
move |_, row: &Value| match row.get_data_by_key(&column_name.item) {
|
||||
Some(group_key) => Ok(group_key.as_string()?),
|
||||
None => Err(ShellError::CantFindColumn(
|
||||
column_name.item.to_string(),
|
||||
column_name.span,
|
||||
row.span().unwrap_or(column_name.span),
|
||||
)),
|
||||
|
@ -137,6 +137,7 @@ pub fn split_by(
|
||||
});
|
||||
Ok(split(&splitter, input, name)?)
|
||||
}
|
||||
// This uses the same format as the 'requires a column name' error in sort_utils.rs
|
||||
None => Err(ShellError::GenericError(
|
||||
"expected name".into(),
|
||||
"requires a column name for splitting".into(),
|
||||
@ -165,6 +166,7 @@ pub fn split(
|
||||
move |_, row: &Value| match row.get_data_by_key(&column_name.item) {
|
||||
Some(group_key) => Ok(group_key.as_string()?),
|
||||
None => Err(ShellError::CantFindColumn(
|
||||
column_name.item.to_string(),
|
||||
column_name.span,
|
||||
row.span().unwrap_or(column_name.span),
|
||||
)),
|
||||
|
@ -1,5 +1,5 @@
|
||||
use alphanumeric_sort::compare_str;
|
||||
use nu_engine::column::column_does_not_exist;
|
||||
use nu_engine::column::nonexistent_column;
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
@ -81,12 +81,18 @@ pub fn sort(
|
||||
..
|
||||
} => {
|
||||
if sort_columns.is_empty() {
|
||||
println!("sort-by requires a column name to sort table data");
|
||||
return Err(ShellError::CantFindColumn(span, span));
|
||||
// This uses the same format as the 'requires a column name' error in split_by.rs
|
||||
return Err(ShellError::GenericError(
|
||||
"expected name".into(),
|
||||
"requires a column name to sort table data".into(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
|
||||
if column_does_not_exist(sort_columns.clone(), cols.to_vec()) {
|
||||
return Err(ShellError::CantFindColumn(span, span));
|
||||
if let Some(nonexistent) = nonexistent_column(sort_columns.clone(), cols.to_vec()) {
|
||||
return Err(ShellError::CantFindColumn(nonexistent, span, span));
|
||||
}
|
||||
|
||||
// check to make sure each value in each column in the record
|
||||
|
@ -24,7 +24,9 @@ fn insert_the_column_conflict() {
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("column already exists"));
|
||||
assert!(actual
|
||||
.err
|
||||
.contains("column 'pretty_assertions' already exists"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -95,3 +95,13 @@ fn update_past_end_list() {
|
||||
|
||||
assert!(actual.err.contains("too large"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_nonexistent_column() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"{a:1} | update b 2"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("cannot find column 'b'"));
|
||||
}
|
||||
|
Reference in New Issue
Block a user