Skip to content

Commit 6932bc0

Browse files
committed
refactor(sdk): Simplify some error matching
By using helper methods on error types. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent d123dc9 commit 6932bc0

4 files changed

Lines changed: 11 additions & 21 deletions

File tree

crates/matrix-sdk/src/authentication/oauth/qrcode/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use oauth2::{
3333
RequestTokenError, StandardErrorResponse,
3434
basic::{BasicErrorResponse, BasicRequestTokenError},
3535
};
36-
use ruma::api::error::{ErrorKind, FromHttpResponseError};
36+
use ruma::api::error::ErrorKind;
3737
use thiserror::Error;
3838
use tokio::sync::Mutex;
3939
use url::Url;
@@ -125,11 +125,8 @@ pub enum QRCodeLoginError {
125125
impl From<SecureChannelError> for QRCodeLoginError {
126126
fn from(e: SecureChannelError) -> Self {
127127
match e {
128-
SecureChannelError::RendezvousChannel(HttpError::Api(ref boxed)) => {
129-
if let FromHttpResponseError::Server(api_error) = boxed.as_ref()
130-
&& let Some(ErrorKind::NotFound) =
131-
api_error.as_client_api_error().and_then(|e| e.error_kind())
132-
{
128+
SecureChannelError::RendezvousChannel(ref http_error) => {
129+
if let Some(ErrorKind::NotFound) = http_error.client_api_error_kind() {
133130
return Self::NotFound;
134131
}
135132
Self::SecureChannel(e)
@@ -195,11 +192,8 @@ pub enum QRCodeGrantLoginError {
195192
impl From<SecureChannelError> for QRCodeGrantLoginError {
196193
fn from(e: SecureChannelError) -> Self {
197194
match e {
198-
SecureChannelError::RendezvousChannel(HttpError::Api(ref boxed)) => {
199-
if let FromHttpResponseError::Server(api_error) = boxed.as_ref()
200-
&& let Some(ErrorKind::NotFound) =
201-
api_error.as_client_api_error().and_then(|e| e.error_kind())
202-
{
195+
SecureChannelError::RendezvousChannel(ref http_error) => {
196+
if let Some(ErrorKind::NotFound) = http_error.client_api_error_kind() {
203197
return Self::NotFound;
204198
}
205199
Self::SecureChannel(e)

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use self::{
9797
verification::{SasVerification, Verification, VerificationRequest},
9898
};
9999
use crate::{
100-
Client, Error, HttpError, Result, Room, RumaApiError, TransmissionProgress,
100+
Client, Error, HttpError, Result, Room, TransmissionProgress,
101101
attachment::Thumbnail,
102102
client::{ClientInner, WeakClient},
103103
cross_process_lock::CrossProcessLockGuard,
@@ -753,8 +753,8 @@ impl Client {
753753
let response = self.keys_upload(r.request_id(), request).await;
754754

755755
if let Err(e) = &response {
756-
match e.as_ruma_api_error() {
757-
Some(RumaApiError::ClientApi(e)) if e.status_code == 400 => {
756+
match e.as_client_api_error() {
757+
Some(e) if e.status_code == 400 => {
758758
if let ErrorBody::Standard(StandardErrorBody { message, .. }) = &e.body
759759
{
760760
// This is one of the nastiest errors we can have. The server

crates/matrix-sdk/src/room/shared_room_history.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ pub(crate) async fn maybe_accept_key_bundle(room: &Room, inviter: &UserId) -> Re
263263
Err(err) => {
264264
// If we encountered an HTTP client error, we should check the status code to
265265
// see if we have been sent a bogus link.
266-
let Some(err) = err
267-
.as_ruma_api_error()
268-
.and_then(|e| e.as_client_api_error())
269-
.and_then(|e| e.error_kind())
270-
else {
266+
let Some(err) = err.client_api_error_kind() else {
271267
// Some other error occurred, which we may be able to recover from at the next
272268
// client startup.
273269
return Ok(());

crates/matrix-sdk/tests/integration/matrix_auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{collections::BTreeMap, sync::Mutex};
22

33
use assert_matches::assert_matches;
44
use matrix_sdk::{
5-
AuthApi, AuthSession, Client, RumaApiError, SessionTokens,
5+
AuthApi, AuthSession, Client, SessionTokens,
66
authentication::matrix::MatrixSession,
77
config::RequestConfig,
88
test_utils::{logged_in_client_with_server, no_retry_test_client_with_server},
@@ -233,7 +233,7 @@ async fn test_login_error() {
233233
.await;
234234

235235
if let Err(err) = client.matrix_auth().login_username("example", "wordpass").send().await {
236-
if let Some(RumaApiError::ClientApi(api_err)) = err.as_ruma_api_error() {
236+
if let Some(api_err) = err.as_client_api_error() {
237237
assert_eq!(api_err.status_code, http::StatusCode::from_u16(403).unwrap());
238238

239239
if let api::error::ErrorBody::Standard(StandardErrorBody { kind, message, .. }) =

0 commit comments

Comments
 (0)