Skip to content

Commit

Permalink
Improve serialize/deserialize errors. Now they include document paths…
Browse files Browse the repository at this point in the history
… when possible
  • Loading branch information
abdolence committed Jun 13, 2024
1 parent 0a9e7f7 commit 3e86d63
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ impl serde::de::Error for FirestoreError {
pub struct FirestoreSerializationError {
pub public: FirestoreErrorPublicGenericDetails,
pub message: String,
pub document_path: Option<String>,
}

impl FirestoreSerializationError {
Expand All @@ -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("-")
)
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/firestore_serde/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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())),
},
);

Expand Down Expand Up @@ -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,
})
}
7 changes: 6 additions & 1 deletion src/firestore_serde/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3e86d63

Please sign in to comment.