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 unrecognized device token in FcmError #3993

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 56 additions & 1 deletion core/notifications/src/executor/fcm/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,60 @@ pub enum FcmError {
#[error("FcmError - I/O Error: {0}")]
IOError(#[from] std::io::Error),
#[error("FcmError - GoogleFcm1Error: {0}")]
GoogleFcm1Error(#[from] google_fcm1::Error),
GoogleFcm1Error(google_fcm1::Error),
#[error("FcmError: UnrecognizedDeviceToken: {0}")]
UnrecognizedDeviceToken(google_fcm1::Error),
}

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"]["status"].as_str() == Some("NOT_FOUND")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if they keys don’t exist? Can we make this safer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if we only want to check for the case where device_tokens are unregistered, then this should just suffice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can match against the error_codes that field should always be present. as specified here fcm v1 error codes

&& value["error"]["details"]
.as_array()
.map_or(false, |details| {
details
.iter()
.any(|detail| detail["errorCode"].as_str() == Some("UNREGISTERED"))
})
{
return FcmError::UnrecognizedDeviceToken(err);
}
FcmError::GoogleFcm1Error(err)
}
_ => FcmError::GoogleFcm1Error(err),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn unrecognized_device_token_err() {
let err_json = json!({
"error": {
"code": 404,
"message": "Requested entity was not found.",
"status": "NOT_FOUND",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "UNREGISTERED"
}
]
}
});

let err = google_fcm1::Error::BadRequest(err_json);
let converted_err: FcmError = err.into();

assert!(matches!(
converted_err,
FcmError::UnrecognizedDeviceToken(_)
));
}
}
2 changes: 1 addition & 1 deletion core/notifications/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Executor {
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::GoogleFcm1Error(google_fcm1::Error::BadRequest(e))) => {
Err(FcmError::UnrecognizedDeviceToken(e)) => {
n_errs += 1;
n_removed_tokens += 1;
error!("BadRequest sending to device: {}", e);
Expand Down
Loading