From 3e86d6357b0de5c4679a76033bb986c3c1c83d21 Mon Sep 17 00:00:00 2001 From: Abdulla Abdurakhmanov Date: Thu, 13 Jun 2024 20:23:46 +0200 Subject: [PATCH] Improve serialize/deserialize errors. Now they include document paths when possible --- src/errors.rs | 7 +++++-- src/firestore_serde/deserializer.rs | 13 +++++++++---- src/firestore_serde/serializer.rs | 7 ++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index ae5df32..98b582b 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -297,6 +297,7 @@ impl serde::de::Error for FirestoreError { pub struct FirestoreSerializationError { pub public: FirestoreErrorPublicGenericDetails, pub message: String, + pub document_path: Option, } impl FirestoreSerializationError { @@ -313,8 +314,10 @@ impl Display for FirestoreSerializationError { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!( f, - "Invalid serialization: {}. {}", - self.public, self.message + "Invalid serialization: {}. {}. Document path: {}", + self.public, + self.message, + self.document_path.as_deref().unwrap_or("-") ) } } diff --git a/src/firestore_serde/deserializer.rs b/src/firestore_serde/deserializer.rs index 7cbc850..9c32903 100644 --- a/src/firestore_serde/deserializer.rs +++ b/src/firestore_serde/deserializer.rs @@ -369,8 +369,8 @@ impl<'de> serde::Deserializer<'de> for FirestoreValue { value_type: Some(gcloud_sdk::google::firestore::v1::value::ValueType::DoubleValue(v.longitude)) }), ] - .into_iter() - .collect(); + .into_iter() + .collect(); visitor.visit_map(FirestoreValueMapAccess::new(lat_lng_fields)) } Some(value::ValueType::TimestampValue(ts)) => { @@ -643,7 +643,7 @@ where fields.insert( "_firestore_full_id".to_string(), gcloud_sdk::google::firestore::v1::Value { - value_type: Some(value::ValueType::StringValue(doc_name)), + value_type: Some(value::ValueType::StringValue(doc_name.clone())), }, ); @@ -671,5 +671,10 @@ where )), }); - T::deserialize(firestore_value) + T::deserialize(firestore_value).map_err(|err| match err { + FirestoreError::DeserializeError(e) => { + FirestoreError::DeserializeError(e.with_document_path(doc_name)) + } + _ => err, + }) } diff --git a/src/firestore_serde/serializer.rs b/src/firestore_serde/serializer.rs index 18714cf..3d41636 100644 --- a/src/firestore_serde/serializer.rs +++ b/src/firestore_serde/serializer.rs @@ -546,7 +546,12 @@ where let serializer = crate::firestore_serde::serializer::FirestoreValueSerializer { none_as_null: false, }; - let document_value = object.serialize(serializer)?; + let document_value = object.serialize(serializer).map_err(|err| match err { + FirestoreError::SerializeError(e) => { + FirestoreError::SerializeError(e.with_document_path(document_path.as_ref().to_string())) + } + _ => err, + })?; match document_value.value.value_type { Some(value::ValueType::MapValue(mv)) => Ok(gcloud_sdk::google::firestore::v1::Document {