From 844be8847274725f80042f55f552ca2caeaf495d Mon Sep 17 00:00:00 2001 From: Matthew Ziter Date: Mon, 4 Feb 2019 15:58:58 -0500 Subject: [PATCH] Handle csv parsing error to fix issue getzola/zola#588 --- .../templates/src/global_fns/load_data.rs | 39 +++++++++++++++++-- components/utils/test-files/uneven_rows.csv | 4 ++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 components/utils/test-files/uneven_rows.csv diff --git a/components/templates/src/global_fns/load_data.rs b/components/templates/src/global_fns/load_data.rs index 4fec8a9e..48b71763 100644 --- a/components/templates/src/global_fns/load_data.rs +++ b/components/templates/src/global_fns/load_data.rs @@ -183,7 +183,7 @@ impl LoadData { pub fn new(content_path: PathBuf, base_path: PathBuf) -> Self { let client = Arc::new(Mutex::new(Client::builder().build().expect("reqwest client build"))); let result_cache = Arc::new(Mutex::new(HashMap::new())); - Self {content_path, base_path, client, result_cache} + Self { content_path, base_path, client, result_cache } } } @@ -291,7 +291,16 @@ fn load_csv(csv_data: String) -> Result { let mut records_array: Vec = Vec::new(); for result in records { - let record = result.unwrap(); + let record = match result { + Ok(r) => r, + Err(e) => { + return Err(tera::Error::chain( + String::from("Error encountered when parsing csv records"), + e, + )); + } + }; + let mut elements_array: Vec = Vec::new(); for e in record.into_iter() { @@ -310,7 +319,7 @@ fn load_csv(csv_data: String) -> Result { #[cfg(test)] mod tests { - use super::{LoadData, DataSource, OutputFormat}; + use super::{DataSource, LoadData, OutputFormat}; use std::collections::HashMap; use std::path::PathBuf; @@ -455,6 +464,30 @@ mod tests { ) } + // Test points to bad csv file with uneven row lengths + #[test] + fn bad_csv_should_result_in_error() { + let static_fn = LoadData::new( + PathBuf::from("../utils/test-files"), + PathBuf::from("../utils/test-files"), + ); + let mut args = HashMap::new(); + args.insert("path".to_string(), to_value("uneven_rows.csv").unwrap()); + let result = static_fn.call(&args.clone()); + + assert!(result.is_err()); + + let error_kind = result.err().unwrap().kind; + match error_kind { + tera::ErrorKind::Msg(msg) => { + if msg != String::from("Error encountered when parsing csv records") { + panic!("Error message is wrong. Perhaps wrong error is being returned?"); + } + } + _ => panic!("Error encountered was not expected CSV error"), + } + } + #[test] fn can_load_json() { let static_fn = LoadData::new( diff --git a/components/utils/test-files/uneven_rows.csv b/components/utils/test-files/uneven_rows.csv new file mode 100644 index 00000000..f2f309c2 --- /dev/null +++ b/components/utils/test-files/uneven_rows.csv @@ -0,0 +1,4 @@ +Number,Title +1,Gutenberg +2,Printing +3,Typewriter,ExtraBadColumn