diff --git a/services/tunnelbroker/rust-lib/src/notifications/apns.rs b/services/tunnelbroker/rust-lib/src/notifications/apns.rs index 49cefba89..5d77e9517 100644 --- a/services/tunnelbroker/rust-lib/src/notifications/apns.rs +++ b/services/tunnelbroker/rust-lib/src/notifications/apns.rs @@ -1,56 +1,56 @@ use crate::ffi::apns_status; use a2::{ Client, Endpoint, Error, ErrorReason::{BadDeviceToken, Unregistered}, NotificationBuilder, NotificationOptions, PlainNotificationBuilder, }; -use anyhow::{anyhow, Result}; +use anyhow::{bail, Result}; use std::fs::File; pub async fn send_by_a2_client( certificate_path: &str, certificate_password: &str, device_token: &str, topic: &str, message: &str, sandbox: bool, ) -> Result { let mut certificate = File::open(certificate_path)?; let endpoint = if sandbox { Endpoint::Sandbox } else { Endpoint::Production }; let client = Client::certificate(&mut certificate, certificate_password, endpoint)?; let options = NotificationOptions { apns_topic: Some(topic), ..Default::default() }; let builder = PlainNotificationBuilder::new(message); let mut payload = builder.build(device_token, options); payload.aps.content_available = Some(1); match client.send(payload).await { Ok(_) => Ok(apns_status::Ok), Err(Error::ResponseError(response)) => { if let Some(error_body) = response.error { match error_body.reason { - // We are returning `Ok` with the error types here to distinguish the exact + // We are returning `Ok` with the error types here to distinguish the exact // error type in a C++ side BadDeviceToken => Ok(apns_status::BadDeviceToken), Unregistered => Ok(apns_status::Unregistered), - _ => Err(anyhow!( + _ => bail!( "Notification was not accepted by APNs, reason: {:?}", error_body.reason - )), + ), } } else { - Err(anyhow!( + bail!( "Unhandled response error from APNs, response: {:?}", response - )) + ) } } Err(error) => Err(anyhow::Error::new(error)), } } diff --git a/services/tunnelbroker/rust-lib/src/notifications/fcm.rs b/services/tunnelbroker/rust-lib/src/notifications/fcm.rs index 5f40b1997..74d176bec 100644 --- a/services/tunnelbroker/rust-lib/src/notifications/fcm.rs +++ b/services/tunnelbroker/rust-lib/src/notifications/fcm.rs @@ -1,48 +1,44 @@ use crate::ffi::fcm_status; -use anyhow::{anyhow, Result}; +use anyhow::{bail, ensure, Result}; use fcm::{ response::ErrorReason::{InvalidRegistration, NotRegistered}, Client, MessageBuilder, NotificationBuilder, }; pub async fn send_by_fcm_client( fcm_api_key: &str, device_registration_id: &str, message_title: &str, message_body: &str, ) -> Result { let client = Client::new(); let mut notification_builder = NotificationBuilder::new(); notification_builder.title(message_title); notification_builder.body(message_body); let notification = notification_builder.finalize(); let mut message_builder = MessageBuilder::new(fcm_api_key, device_registration_id); message_builder.notification(notification); let result = client.send(message_builder.finalize()).await?; match result.results { Some(results) => { - if results.len() == 0 { - return Err(anyhow!("FCM client returned zero size results")); - } + ensure!(results.len() > 0, "FCM client returned zero size results"); if let Some(result_error) = results[0].error { match result_error { // We are returning `Ok` with the error types here to distinguish the exact // error type in a C++ side InvalidRegistration => return Ok(fcm_status::InvalidRegistration), NotRegistered => return Ok(fcm_status::NotRegistered), - _ => { - return Err(anyhow!( - "Notification was not accepted by FCM, reason: {:?}", - result_error - )) - } + _ => bail!( + "Notification was not accepted by FCM, reason: {:?}", + result_error, + ), } } } - None => return Err(anyhow!("FCM client has no results set")), + None => bail!("FCM client has no results set"), } Ok(fcm_status::Ok) }