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

feat: propagate deserialize error #34

Merged
merged 1 commit into from
Dec 28, 2023
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "firebase-messaging-rs"
version = "0.6.1"
version = "0.7.0"
authors = [
"Yoichiro ITO <[email protected]>"
]
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod topic;

use async_trait::async_trait;
use gcloud_sdk::{GoogleAuthTokenGenerator, TokenSourceType, GCP_DEFAULT_SCOPES};
use gcloud_sdk::{
google::devtools::source, GoogleAuthTokenGenerator, TokenSourceType, GCP_DEFAULT_SCOPES,
};
use http::{
header::{ACCEPT, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE},
Request, Response, StatusCode,
Expand Down Expand Up @@ -147,8 +149,12 @@ pub trait GenericGoogleRestAPISupport {
.await
.map_err(|_| RPCError::DecodeFailure)
.map_err(E::from)?;
let text = std::str::from_utf8(&buf).unwrap_or_default();
serde_json::from_slice::<R>(&buf)
.map_err(|_| RPCError::DeserializeFailure)
.map_err(|e| RPCError::DeserializeFailure {
reason: format!("{e:?}"),
source: text.to_string(),
})
.map_err(E::from)
}
StatusCode::UNAUTHORIZED => {
Expand All @@ -171,7 +177,7 @@ pub enum RPCError {
BuildRequestFailure(String),
HttpRequestFailure,
DecodeFailure,
DeserializeFailure,
DeserializeFailure { reason: String, source: String },
InvalidRequest,
Internal,
Unknown(u16),
Expand Down
4 changes: 2 additions & 2 deletions src/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl From<RPCError> for TopicManagementError {
RPCError::DecodeFailure => Self::InternalResponseError {
msg: "unable to decode response body bytes".to_string(),
},
RPCError::DeserializeFailure => Self::InternalResponseError {
msg: "unable to deserialize response body to type".to_string(),
RPCError::DeserializeFailure { reason, source } => Self::InternalResponseError {
msg: format!("unable to deserialize response body to type: {reason}: {source}"),
},
RPCError::Unauthorized(msg) => Self::Unauthorized(msg),
RPCError::InvalidRequest => Self::InvalidRequest,
Expand Down
Loading