Skip to content

Commit

Permalink
fix(notifications): use safe access methods for JSON parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Feb 14, 2024
1 parent a186dae commit b7035ed
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions core/notifications/src/executor/fcm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ impl From<google_fcm1::Error> for FcmError {
fn from(err: google_fcm1::Error) -> Self {
match err {
google_fcm1::Error::BadRequest(ref value) => {
if value["error"]["code"].as_u64() == Some(404)
&& value["error"]["status"].as_str() == Some("NOT_FOUND")
&& value["error"]["details"]
.as_array()
.map_or(false, |details| {
details
.iter()
.any(|detail| detail["errorCode"].as_str() == Some("UNREGISTERED"))
let code = value
.get("error")
.and_then(|e| e.get("code"))
.and_then(|c| c.as_u64());
let status = value
.get("error")
.and_then(|e| e.get("status"))
.and_then(|s| s.as_str());
let is_unregistered = value
.get("error")
.and_then(|e| e.get("details"))
.and_then(|d| d.as_array())
.map_or(false, |details| {
details.iter().any(|detail| {
detail.get("errorCode").and_then(|e| e.as_str()) == Some("UNREGISTERED")
})
{
return FcmError::UnrecognizedDeviceToken(err);
});

if let (Some(code), Some(status)) = (code, status) {
if code == 404 && status == "NOT_FOUND" && is_unregistered {
return FcmError::UnrecognizedDeviceToken(err);
}
}
FcmError::GoogleFcm1Error(err)
}
Expand Down

0 comments on commit b7035ed

Please sign in to comment.