Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve serialize/deserialize errors. Now they include document paths when possible #183

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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