Box the large GeoError error variant

This commit is contained in:
Clément Renault 2025-04-01 11:26:17 +02:00
parent 4d90e3d2ec
commit 64477aac60
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F
5 changed files with 45 additions and 24 deletions

View File

@ -151,7 +151,7 @@ and can not be more than 511 bytes.", .document_id.to_string()
matching_rule_indices: HashMap<String, usize>, matching_rule_indices: HashMap<String, usize>,
}, },
#[error(transparent)] #[error(transparent)]
InvalidGeoField(#[from] GeoError), InvalidGeoField(#[from] Box<GeoError>),
#[error("Invalid vector dimensions: expected: `{}`, found: `{}`.", .expected, .found)] #[error("Invalid vector dimensions: expected: `{}`, found: `{}`.", .expected, .found)]
InvalidVectorDimensions { expected: usize, found: usize }, InvalidVectorDimensions { expected: usize, found: usize },
#[error("The `_vectors` field in the document with id: `{document_id}` is not an object. Was expecting an object with a key for each embedder with manually provided vectors, but instead got `{value}`")] #[error("The `_vectors` field in the document with id: `{document_id}` is not an object. Was expecting an object with a key for each embedder with manually provided vectors, but instead got `{value}`")]
@ -519,7 +519,7 @@ error_from_sub_error! {
str::Utf8Error => InternalError, str::Utf8Error => InternalError,
ThreadPoolBuildError => InternalError, ThreadPoolBuildError => InternalError,
SerializationError => InternalError, SerializationError => InternalError,
GeoError => UserError, Box<GeoError> => UserError,
CriterionError => UserError, CriterionError => UserError,
} }

View File

@ -2954,10 +2954,15 @@ pub(crate) mod tests {
documents!({ "id" : 6, RESERVED_GEO_FIELD_NAME: {"lat": "unparseable", "lng": "unparseable"}}), documents!({ "id" : 6, RESERVED_GEO_FIELD_NAME: {"lat": "unparseable", "lng": "unparseable"}}),
) )
.unwrap_err(); .unwrap_err();
assert!(matches!( match err1 {
err1, Error::UserError(UserError::InvalidGeoField(err)) => match *err {
Error::UserError(UserError::InvalidGeoField(GeoError::BadLatitudeAndLongitude { .. })) GeoError::BadLatitudeAndLongitude { .. } => (),
)); otherwise => {
panic!("err1 is not a BadLatitudeAndLongitude error but rather a {otherwise:?}")
}
},
_ => panic!("err1 is not a BadLatitudeAndLongitude error but rather a {err1:?}"),
}
db_snap!(index, geo_faceted_documents_ids); // ensure that no more document was inserted db_snap!(index, geo_faceted_documents_ids); // ensure that no more document was inserted
} }

View File

@ -115,7 +115,7 @@ pub fn enrich_documents_batch<R: Read + Seek>(
if let Some(geo_value) = geo_field_id.and_then(|fid| document.get(fid)) { if let Some(geo_value) = geo_field_id.and_then(|fid| document.get(fid)) {
if let Err(user_error) = validate_geo_from_json(&document_id, geo_value)? { if let Err(user_error) = validate_geo_from_json(&document_id, geo_value)? {
return Ok(Err(UserError::from(user_error))); return Ok(Err(UserError::from(Box::new(user_error))));
} }
} }

View File

@ -80,22 +80,28 @@ fn extract_lat_lng(
let (lat, lng) = match (lat, lng) { let (lat, lng) = match (lat, lng) {
(Some(lat), Some(lng)) => (lat, lng), (Some(lat), Some(lng)) => (lat, lng),
(Some(_), None) => { (Some(_), None) => {
return Err(GeoError::MissingLatitude { document_id: document_id() }.into()) return Err(
Box::new(GeoError::MissingLatitude { document_id: document_id() }).into()
)
} }
(None, Some(_)) => { (None, Some(_)) => {
return Err(GeoError::MissingLongitude { document_id: document_id() }.into()) return Err(
Box::new(GeoError::MissingLongitude { document_id: document_id() }).into()
)
} }
(None, None) => return Ok(None), (None, None) => return Ok(None),
}; };
let lat = extract_finite_float_from_value( let lat = extract_finite_float_from_value(
serde_json::from_slice(lat).map_err(InternalError::SerdeJson)?, serde_json::from_slice(lat).map_err(InternalError::SerdeJson)?,
) )
.map_err(|lat| GeoError::BadLatitude { document_id: document_id(), value: lat })?; .map_err(|lat| GeoError::BadLatitude { document_id: document_id(), value: lat })
.map_err(Box::new)?;
let lng = extract_finite_float_from_value( let lng = extract_finite_float_from_value(
serde_json::from_slice(lng).map_err(InternalError::SerdeJson)?, serde_json::from_slice(lng).map_err(InternalError::SerdeJson)?,
) )
.map_err(|lng| GeoError::BadLongitude { document_id: document_id(), value: lng })?; .map_err(|lng| GeoError::BadLongitude { document_id: document_id(), value: lng })
.map_err(Box::new)?;
Ok(Some([lat, lng])) Ok(Some([lat, lng]))
} }
None => Ok(None), None => Ok(None),

View File

@ -258,9 +258,11 @@ pub fn extract_geo_coordinates(
Value::Null => return Ok(None), Value::Null => return Ok(None),
Value::Object(map) => map, Value::Object(map) => map,
value => { value => {
return Err( return Err(Box::new(GeoError::NotAnObject {
GeoError::NotAnObject { document_id: Value::from(external_id), value }.into() document_id: Value::from(external_id),
) value,
})
.into())
} }
}; };
@ -269,23 +271,29 @@ pub fn extract_geo_coordinates(
if geo.is_empty() { if geo.is_empty() {
[lat, lng] [lat, lng]
} else { } else {
return Err(GeoError::UnexpectedExtraFields { return Err(Box::new(GeoError::UnexpectedExtraFields {
document_id: Value::from(external_id), document_id: Value::from(external_id),
value: Value::from(geo), value: Value::from(geo),
} })
.into()); .into());
} }
} }
(Some(_), None) => { (Some(_), None) => {
return Err(GeoError::MissingLongitude { document_id: Value::from(external_id) }.into()) return Err(Box::new(GeoError::MissingLongitude {
document_id: Value::from(external_id),
})
.into())
} }
(None, Some(_)) => { (None, Some(_)) => {
return Err(GeoError::MissingLatitude { document_id: Value::from(external_id) }.into()) return Err(Box::new(GeoError::MissingLatitude {
document_id: Value::from(external_id),
})
.into())
} }
(None, None) => { (None, None) => {
return Err(GeoError::MissingLatitudeAndLongitude { return Err(Box::new(GeoError::MissingLatitudeAndLongitude {
document_id: Value::from(external_id), document_id: Value::from(external_id),
} })
.into()) .into())
} }
}; };
@ -293,16 +301,18 @@ pub fn extract_geo_coordinates(
match (extract_finite_float_from_value(lat), extract_finite_float_from_value(lng)) { match (extract_finite_float_from_value(lat), extract_finite_float_from_value(lng)) {
(Ok(lat), Ok(lng)) => Ok(Some([lat, lng])), (Ok(lat), Ok(lng)) => Ok(Some([lat, lng])),
(Ok(_), Err(value)) => { (Ok(_), Err(value)) => {
Err(GeoError::BadLongitude { document_id: Value::from(external_id), value }.into()) Err(Box::new(GeoError::BadLongitude { document_id: Value::from(external_id), value })
.into())
} }
(Err(value), Ok(_)) => { (Err(value), Ok(_)) => {
Err(GeoError::BadLatitude { document_id: Value::from(external_id), value }.into()) Err(Box::new(GeoError::BadLatitude { document_id: Value::from(external_id), value })
.into())
} }
(Err(lat), Err(lng)) => Err(GeoError::BadLatitudeAndLongitude { (Err(lat), Err(lng)) => Err(Box::new(GeoError::BadLatitudeAndLongitude {
document_id: Value::from(external_id), document_id: Value::from(external_id),
lat, lat,
lng, lng,
} })
.into()), .into()),
} }
} }