Skip to content

Commit

Permalink
feat: propagate deserialize error
Browse files Browse the repository at this point in the history
  • Loading branch information
i10416 committed Dec 28, 2023
1 parent e3d77d8 commit afd0322
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
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

0 comments on commit afd0322

Please sign in to comment.