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 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
69 changes: 68 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,72 @@ 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) => {
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")
})
});

if let (Some(code), Some(status)) = (code, status) {
if code == 404 && status == "NOT_FOUND" && is_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