diff --git a/milli/src/documents/builder.rs b/milli/src/documents/builder.rs index 144dcdfa9..c2b0e01cc 100644 --- a/milli/src/documents/builder.rs +++ b/milli/src/documents/builder.rs @@ -115,14 +115,17 @@ impl DocumentBatchBuilder { .map(|(k, t)| (this.index.insert(&k), t)) .collect::>(); - let records = records.into_records(); - - for record in records { + for (i, record) in records.into_records().enumerate() { let record = record?; let mut writer = obkv::KvWriter::new(Cursor::new(&mut this.obkv_buffer)); for (value, (fid, ty)) in record.into_iter().zip(headers.iter()) { let value = match ty { - AllowedType::Number => value.parse::().map(Value::from)?, + AllowedType::Number => value.parse::().map(Value::from).map_err(|error| Error::ParseFloat { + error, + // +1 for the header offset. + line: i + 1, + value: value.to_string(), + })?, AllowedType::String => Value::String(value.to_string()), }; diff --git a/milli/src/documents/mod.rs b/milli/src/documents/mod.rs index b61a3326b..6f653d825 100644 --- a/milli/src/documents/mod.rs +++ b/milli/src/documents/mod.rs @@ -83,7 +83,11 @@ impl io::Write for ByteCounter { #[derive(Debug)] pub enum Error { - ParseFloat(std::num::ParseFloatError), + ParseFloat { + error: std::num::ParseFloatError, + line: usize, + value: String, + }, InvalidDocumentFormat, Custom(String), JsonError(serde_json::Error), @@ -117,16 +121,10 @@ impl From for Error { } } -impl From for Error { - fn from(other: ParseFloatError) -> Self { - Self::ParseFloat(other) - } -} - impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Error::ParseFloat(e) => write!(f, "{}", e), + Error::ParseFloat { error, line, value} => write!(f, "Error parsing number {:?} at line {}: {}", value, line, error), Error::Custom(s) => write!(f, "Unexpected serialization error: {}", s), Error::InvalidDocumentFormat => f.write_str("Invalid document addition format."), Error::JsonError(err) => write!(f, "Couldn't serialize document value: {}", err),