return float parsing error context in csv

This commit is contained in:
marin postma 2021-10-25 16:07:57 +02:00
parent 3fcccc31b5
commit f9445c1d90
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
2 changed files with 13 additions and 12 deletions

View File

@ -115,14 +115,17 @@ impl<W: io::Write + io::Seek> DocumentBatchBuilder<W> {
.map(|(k, t)| (this.index.insert(&k), t)) .map(|(k, t)| (this.index.insert(&k), t))
.collect::<BTreeMap<_, _>>(); .collect::<BTreeMap<_, _>>();
let records = records.into_records(); for (i, record) in records.into_records().enumerate() {
for record in records {
let record = record?; let record = record?;
let mut writer = obkv::KvWriter::new(Cursor::new(&mut this.obkv_buffer)); let mut writer = obkv::KvWriter::new(Cursor::new(&mut this.obkv_buffer));
for (value, (fid, ty)) in record.into_iter().zip(headers.iter()) { for (value, (fid, ty)) in record.into_iter().zip(headers.iter()) {
let value = match ty { let value = match ty {
AllowedType::Number => value.parse::<f64>().map(Value::from)?, AllowedType::Number => value.parse::<f64>().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()), AllowedType::String => Value::String(value.to_string()),
}; };

View File

@ -83,7 +83,11 @@ impl<W: io::Write> io::Write for ByteCounter<W> {
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
ParseFloat(std::num::ParseFloatError), ParseFloat {
error: std::num::ParseFloatError,
line: usize,
value: String,
},
InvalidDocumentFormat, InvalidDocumentFormat,
Custom(String), Custom(String),
JsonError(serde_json::Error), JsonError(serde_json::Error),
@ -117,16 +121,10 @@ impl From<serde_json::Error> for Error {
} }
} }
impl From<ParseFloatError> for Error {
fn from(other: ParseFloatError) -> Self {
Self::ParseFloat(other)
}
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { 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::Custom(s) => write!(f, "Unexpected serialization error: {}", s),
Error::InvalidDocumentFormat => f.write_str("Invalid document addition format."), Error::InvalidDocumentFormat => f.write_str("Invalid document addition format."),
Error::JsonError(err) => write!(f, "Couldn't serialize document value: {}", err), Error::JsonError(err) => write!(f, "Couldn't serialize document value: {}", err),