diff --git a/services/tunnelbroker/rust-lib/src/notifications/apns.rs b/services/tunnelbroker/rust-lib/src/notifications/apns.rs --- a/services/tunnelbroker/rust-lib/src/notifications/apns.rs +++ b/services/tunnelbroker/rust-lib/src/notifications/apns.rs @@ -4,7 +4,7 @@ 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( @@ -35,20 +35,20 @@ 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 --- a/services/tunnelbroker/rust-lib/src/notifications/fcm.rs +++ b/services/tunnelbroker/rust-lib/src/notifications/fcm.rs @@ -1,5 +1,5 @@ use crate::ffi::fcm_status; -use anyhow::{anyhow, Result}; +use anyhow::{bail, ensure, Result}; use fcm::{ response::ErrorReason::{InvalidRegistration, NotRegistered}, Client, MessageBuilder, NotificationBuilder, @@ -24,25 +24,21 @@ 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) }