From 9c7b25134b7d0ad96f472387f5bec13f9921f0d1 Mon Sep 17 00:00:00 2001 From: Dietrich Daroch Date: Sun, 6 Dec 2020 15:19:04 -0300 Subject: [PATCH] Parsing: Explain parsing errors and show sample lines. (#2774) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the errors slightly better. It took me a while to realize I was missing the `--raw` flag. ``` open "data.csv" | from csv --separator ';' ``` error: Could not parse as CSV split by ',' (Line 1: expected 1 fields, found 14) ┌─ shell:1:1 │ 1 │ open "data.csv" | from csv --separator ';' │ ^^^^ ------------------------------------------------- value originates from here │ │ │ input cannot be parsed as CSV split by ','. Sample input: Name;Data Ugly;row AnotherUgly;row I think this still needs some refinement. Maybe we don't want to show the separator all the time, omitting the defaults or the separator on other formats. --- crates/nu-cli/src/commands/from_delimited_data.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/commands/from_delimited_data.rs b/crates/nu-cli/src/commands/from_delimited_data.rs index 3e2c26730..fb4eb9cfa 100644 --- a/crates/nu-cli/src/commands/from_delimited_data.rs +++ b/crates/nu-cli/src/commands/from_delimited_data.rs @@ -54,6 +54,7 @@ pub async fn from_delimited_data( ) -> Result { let name_tag = name; let concat_string = input.collect_string(name_tag.clone()).await?; + let sample_lines = concat_string.item.lines().take(3).collect_vec().join("\n"); match from_delimited_string_to_value(concat_string.item, headerless, sep, name_tag.clone()) { Ok(x) => match x { @@ -65,10 +66,16 @@ pub async fn from_delimited_data( }, Err(err) => { let line_one = match pretty_csv_error(err) { - Some(pretty) => format!("Could not parse as {} ({})", format_name, pretty), - None => format!("Could not parse as {}", format_name), + Some(pretty) => format!( + "Could not parse as {} split by '{}' ({})", + format_name, sep, pretty + ), + None => format!("Could not parse as {} split by '{}'", format_name, sep), }; - let line_two = format!("input cannot be parsed as {}", format_name); + let line_two = format!( + "input cannot be parsed as {} split by '{}'. Input's first lines:\n{}", + format_name, sep, sample_lines + ); Err(ShellError::labeled_error_with_secondary( line_one,