Skip to content

Commit

Permalink
fix(notifications): handle invalid device tokens error (#4017)
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit authored Feb 18, 2024
1 parent e02da1c commit 43fa9c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion core/notifications/src/push_executor/fcm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum FcmError {
GoogleFcm1Error(google_fcm1::Error),
#[error("FcmError: UnrecognizedDeviceToken: {0}")]
UnrecognizedDeviceToken(google_fcm1::Error),
#[error("FcmError: InvalidDeviceToken: {0}")]
InvalidDeviceToken(google_fcm1::Error),
}

impl From<google_fcm1::Error> for FcmError {
Expand All @@ -31,10 +33,21 @@ impl From<google_fcm1::Error> for FcmError {
detail.get("errorCode").and_then(|e| e.as_str()) == Some("UNREGISTERED")
})
});
let message = value
.get("error")
.and_then(|e| e.get("message"))
.and_then(|m| m.as_str());

if let (Some(code), Some(status)) = (code, status) {
if let (Some(code), Some(status), Some(msg)) = (code, status, message) {
if code == 404 && status == "NOT_FOUND" && is_unregistered {
return FcmError::UnrecognizedDeviceToken(err);
} else if code == 400
&& status == "INVALID_ARGUMENT"
&& msg.contains(
"The registration token is not a valid FCM registration token",
)
{
return FcmError::InvalidDeviceToken(err);
}
}
FcmError::GoogleFcm1Error(err)
Expand Down Expand Up @@ -73,4 +86,25 @@ mod tests {
FcmError::UnrecognizedDeviceToken(_)
));
}

#[test]
fn invalid_device_token_err() {
let err_json = json!({
"error": {
"code": 400,
"message": "The registration token is not a valid FCM registration token",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
}
]
}
});
let err = google_fcm1::Error::BadRequest(err_json);
let converted_err: FcmError = err.into();

assert!(matches!(converted_err, FcmError::InvalidDeviceToken(_)));
}
}
2 changes: 1 addition & 1 deletion core/notifications/src/push_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl PushExecutor {
let mut n_removed_tokens = 0;
for device_token in settings.push_device_tokens() {
match self.fcm.send(&device_token, &msg, event.deep_link()).await {
Err(FcmError::UnrecognizedDeviceToken(e)) => {
Err(FcmError::UnrecognizedDeviceToken(e) | FcmError::InvalidDeviceToken(e)) => {
n_errs += 1;
n_removed_tokens += 1;
error!("BadRequest sending to device: {}", e);
Expand Down

0 comments on commit 43fa9c1

Please sign in to comment.