Skip to content

Commit

Permalink
Introduce trait to share error handling between PushService and WebSo…
Browse files Browse the repository at this point in the history
…cketService
  • Loading branch information
gferon committed Oct 17, 2024
1 parent 49baa11 commit 5c99403
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 268 deletions.
16 changes: 12 additions & 4 deletions src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ impl AccountManager {
"/v1/devices/provisioning/code",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand Down Expand Up @@ -259,7 +261,9 @@ impl AccountManager {
.json(&ProvisioningMessage {
body: BASE64_RELAXED.encode(body),
})
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

Ok(())
Expand Down Expand Up @@ -597,7 +601,9 @@ impl AccountManager {
.json(&Data {
device_name: encrypted_device_name.encode_to_vec(),
})
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

Ok(())
Expand Down Expand Up @@ -626,7 +632,9 @@ impl AccountManager {
token: String::from(token),
captcha: String::from(captcha),
})
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/groups_v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ impl<C: CredentialsCache> GroupsManager<C> {
&path,
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand Down
4 changes: 3 additions & 1 deletion src/profile_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ impl ProfileService {
path,
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand Down
16 changes: 12 additions & 4 deletions src/push_service/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use reqwest::Method;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::{HttpAuthOverride, PushService, ReqwestExt, ServiceError};
use super::{
response::ReqwestExt, HttpAuthOverride, PushService, ServiceError,
};
use crate::{
configuration::Endpoint,
utils::{serde_optional_base64, serde_phone_number},
Expand Down Expand Up @@ -134,7 +136,9 @@ impl PushService {
"/v1/accounts/whoami",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand All @@ -157,7 +161,9 @@ impl PushService {
"/v1/devices/",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand All @@ -181,7 +187,9 @@ impl PushService {
HttpAuthOverride::NoOverride,
)?
.json(&attributes)
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand Down
14 changes: 10 additions & 4 deletions src/push_service/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
proto::AttachmentPointer, push_service::HttpAuthOverride,
};

use super::{PushService, ReqwestExt, ServiceError};
use super::{response::ReqwestExt, PushService, ServiceError};

#[derive(Debug, serde::Deserialize, Default)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -53,7 +53,9 @@ impl PushService {
path,
HttpAuthOverride::Unidentified, // CDN requests are always without authentication
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.bytes_stream()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
Expand All @@ -71,7 +73,9 @@ impl PushService {
"/v2/attachments/form/upload",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand Down Expand Up @@ -130,7 +134,9 @@ impl PushService {
HttpAuthOverride::NoOverride,
)?
.multipart(form)
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

debug!("HyperPushService::PUT response: {:?}", response);
Expand Down
4 changes: 2 additions & 2 deletions src/push_service/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub enum ServiceError {
#[error("Error decoding response: {reason}")]
ResponseError { reason: String },

#[error("Error decoding JSON response: {reason}")]
JsonDecodeError { reason: String },
#[error("Error decoding JSON: {0}")]
JsonDecodeError(#[from] serde_json::Error),
#[error("Error decoding protobuf frame: {0}")]
ProtobufDecodeError(#[from] prost::DecodeError),
#[error("error encoding or decoding bincode: {0}")]
Expand Down
30 changes: 22 additions & 8 deletions src/push_service/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};

use super::{
HttpAuthOverride, PushService, ReqwestExt, SenderCertificateJson,
response::ReqwestExt, HttpAuthOverride, PushService, SenderCertificateJson,
ServiceError, ServiceIdType, VerifyAccountResponse,
};

Expand All @@ -36,7 +36,9 @@ impl PushService {
&format!("/v2/keys?identity={}", service_id_type),

Check warning on line 36 in src/push_service/keys.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> src/push_service/keys.rs:36:13 | 36 | &format!("/v2/keys?identity={}", service_id_type), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `format!("/v2/keys?identity={}", service_id_type)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand All @@ -55,7 +57,9 @@ impl PushService {
HttpAuthOverride::NoOverride,
)?
.json(&pre_key_state)
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

Ok(())
Expand All @@ -76,7 +80,9 @@ impl PushService {
&path,
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand Down Expand Up @@ -105,7 +111,9 @@ impl PushService {
&path,
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand All @@ -127,7 +135,9 @@ impl PushService {
"/v1/certificate/delivery",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand All @@ -144,7 +154,9 @@ impl PushService {
"/v1/certificate/delivery?includeE164=false",
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await?;
Expand Down Expand Up @@ -190,7 +202,9 @@ impl PushService {
pni_registration_ids,
signature_valid_on_each_signed_pre_key,
})
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand Down
12 changes: 8 additions & 4 deletions src/push_service/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use uuid::Uuid;
use crate::configuration::Endpoint;

use super::{
DeviceActivationRequest, HttpAuth, HttpAuthOverride, PushService,
ReqwestExt, ServiceError,
response::ReqwestExt, DeviceActivationRequest, HttpAuth, HttpAuthOverride,
PushService, ServiceError,
};

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -67,7 +67,9 @@ impl PushService {
HttpAuthOverride::Identified(http_auth),
)?
.json(&link_request)
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?
.json()
.await
Expand All @@ -81,7 +83,9 @@ impl PushService {
format!("/v1/devices/{}", id),
HttpAuthOverride::NoOverride,
)?
.send_to_signal()
.send()
.await?
.service_error_for_status()
.await?;

Ok(())
Expand Down
Loading

0 comments on commit 5c99403

Please sign in to comment.