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

fix(notifications): handle invalid device tokens error #4017

Merged
merged 1 commit into from
Feb 18, 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
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
Loading