diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 452b3491a..fdffe5271 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -5,22 +5,19 @@ use matrix_sdk::{ config::SyncSettings, media::{MediaFormat, MediaRequest, MediaThumbnailSize}, ruma::{ - api::{ - client::{ - account::whoami, - error::ErrorKind, - filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter}, - media::get_content_thumbnail::v3::Method, - session::get_login_types, - sync::sync_events::v3::Filter, - }, - error::{FromHttpResponseError, ServerError}, + api::client::{ + account::whoami, + error::ErrorKind, + filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter}, + media::get_content_thumbnail::v3::Method, + session::get_login_types, + sync::sync_events::v3::Filter, }, events::room::MediaSource, serde::Raw, TransactionId, UInt, }, - Client as MatrixClient, Error, HttpError, LoopCtrl, RumaApiError, Session, + Client as MatrixClient, Error, LoopCtrl, RumaApiError, Session, }; use super::{ @@ -268,10 +265,7 @@ impl Client { /// Process a sync error and return loop control accordingly fn process_sync_error(&self, sync_error: Error) -> LoopCtrl { let mut control = LoopCtrl::Continue; - if let Error::Http(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(error), - )))) = sync_error - { + if let Some(RumaApiError::ClientApi(error)) = sync_error.as_ruma_api_error() { if let ErrorKind::UnknownToken { soft_logout } = error.kind { self.state.write().unwrap().is_soft_logout = soft_logout; if let Some(delegate) = &*self.delegate.read().unwrap() { diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index ec9fb18dd..6d164cd69 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -58,7 +58,7 @@ use ruma::{ sync::sync_events, uiaa::{AuthData, UserIdentifier}, }, - error::{FromHttpResponseError, ServerError}, + error::FromHttpResponseError, MatrixVersion, OutgoingRequest, SendAccessToken, }, assign, DeviceId, OwnedDeviceId, OwnedRoomId, OwnedServerName, RoomAliasId, RoomId, @@ -1338,13 +1338,11 @@ impl Client { Ok(Some(res)) } Err(error) => { - *guard = if let HttpError::Api(FromHttpResponseError::Server( - ServerError::Known(RumaApiError::ClientApi(api_error)), - )) = &error - { - Err(RefreshTokenError::ClientApi(api_error.to_owned())) - } else { - Err(RefreshTokenError::UnableToRefreshToken) + *guard = match error.as_ruma_api_error() { + Some(RumaApiError::ClientApi(api_error)) => { + Err(RefreshTokenError::ClientApi(api_error.to_owned())) + } + _ => Err(RefreshTokenError::UnableToRefreshToken), }; Err(error) @@ -1688,9 +1686,9 @@ impl Client { // If this is an `M_UNKNOWN_TOKEN` error and refresh token handling is active, // try to refresh the token and retry the request. if self.inner.handle_refresh_tokens { - if let Err(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(error), - )))) = &res + // FIXME: Use if-let chain once available + if let Err(Some(RumaApiError::ClientApi(error))) = + res.as_ref().map_err(HttpError::as_ruma_api_error) { if matches!(error.kind, ErrorKind::UnknownToken { .. }) { let refresh_res = self.refresh_access_token().await; @@ -1733,9 +1731,9 @@ impl Client { // If this is an `M_UNKNOWN_TOKEN` error and refresh token handling is active, // try to refresh the token and retry the request. if self.inner.handle_refresh_tokens { - if let Err(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(error), - )))) = &res + // FIXME: Use if-let chain once available + if let Err(Some(RumaApiError::ClientApi(error))) = + res.as_ref().map_err(HttpError::as_ruma_api_error) { if matches!(error.kind, ErrorKind::UnknownToken { .. }) { let refresh_res = self.refresh_access_token().await; diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index 1e56ff766..ad28e05de 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -253,13 +253,9 @@ impl HttpError { /// This method is an convenience method to get to the info the server /// returned on the first, failed request. pub fn uiaa_response(&self) -> Option<&UiaaInfo> { - if let HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::Uiaa(UiaaResponse::AuthResponse(i)), - ))) = self - { - Some(i) - } else { - None + match self.as_ruma_api_error() { + Some(RumaApiError::Uiaa(UiaaResponse::AuthResponse(i))) => Some(i), + _ => None, } } } @@ -277,13 +273,9 @@ impl Error { /// This method is an convenience method to get to the info the server /// returned on the first, failed request. pub fn uiaa_response(&self) -> Option<&UiaaInfo> { - if let Error::Http(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::Uiaa(UiaaResponse::AuthResponse(i)), - )))) = self - { - Some(i) - } else { - None + match self.as_ruma_api_error() { + Some(RumaApiError::Uiaa(UiaaResponse::AuthResponse(i))) => Some(i), + _ => None, } } } diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index 8286b9aef..ef69fb287 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -3,23 +3,20 @@ use std::{collections::BTreeMap, str::FromStr, time::Duration}; use matrix_sdk::{ config::SyncSettings, media::{MediaFormat, MediaRequest, MediaThumbnailSize}, - Error, HttpError, RumaApiError, Session, + RumaApiError, Session, }; use matrix_sdk_test::{async_test, test_json}; use ruma::{ - api::{ - client::{ - self as client_api, - account::register::{v3::Request as RegistrationRequest, RegistrationKind}, - directory::{ - get_public_rooms, - get_public_rooms_filtered::{self, v3::Request as PublicRoomsFilterRequest}, - }, - media::get_content_thumbnail::v3::Method, - session::get_login_types::v3::LoginType, - uiaa::{self, UiaaResponse}, + api::client::{ + self as client_api, + account::register::{v3::Request as RegistrationRequest, RegistrationKind}, + directory::{ + get_public_rooms, + get_public_rooms_filtered::{self, v3::Request as PublicRoomsFilterRequest}, }, - error::{FromHttpResponseError, ServerError}, + media::get_content_thumbnail::v3::Method, + session::get_login_types::v3::LoginType, + uiaa::{self, UiaaResponse}, }, assign, device_id, directory::Filter, @@ -188,18 +185,17 @@ async fn login_error() { .await; if let Err(err) = client.login_username("example", "wordpass").send().await { - if let Error::Http(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(client_api::Error { kind, message, status_code }), - )))) = err + if let Some(RumaApiError::ClientApi(client_api::Error { kind, message, status_code })) = + err.as_ruma_api_error() { - if let client_api::error::ErrorKind::Forbidden = kind { - } else { - panic!("found the wrong `ErrorKind` {:?}, expected `Forbidden", kind); + if *kind != client_api::error::ErrorKind::Forbidden { + panic!("found the wrong `ErrorKind` {kind:?}, expected `Forbidden"); } - assert_eq!(message, "Invalid password".to_owned()); - assert_eq!(status_code, http::StatusCode::from_u16(403).unwrap()); + + assert_eq!(message, "Invalid password"); + assert_eq!(*status_code, http::StatusCode::from_u16(403).unwrap()); } else { - panic!("found the wrong `Error` type {:?}, expected `Error::RumaResponse", err); + panic!("found the wrong `Error` type {err:?}, expected `Error::RumaResponse"); } } else { panic!("this request should return an `Err` variant") @@ -228,22 +224,20 @@ async fn register_error() { }); if let Err(err) = client.register(user).await { - if let HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::Uiaa(UiaaResponse::MatrixError(client_api::Error { - kind, - message, - status_code, - })), - ))) = err + if let Some(RumaApiError::Uiaa(UiaaResponse::MatrixError(client_api::Error { + kind, + message, + status_code, + }))) = err.as_ruma_api_error() { - if let client_api::error::ErrorKind::Forbidden = kind { - } else { - panic!("found the wrong `ErrorKind` {:?}, expected `Forbidden", kind); + if *kind != client_api::error::ErrorKind::Forbidden { + panic!("found the wrong `ErrorKind` {kind:?}, expected `Forbidden"); } - assert_eq!(message, "Invalid password".to_owned()); - assert_eq!(status_code, http::StatusCode::from_u16(403).unwrap()); + + assert_eq!(message, "Invalid password"); + assert_eq!(*status_code, http::StatusCode::from_u16(403).unwrap()); } else { - panic!("found the wrong `Error` type {:#?}, expected `UiaaResponse`", err); + panic!("found the wrong `Error` type {err:#?}, expected `UiaaResponse`"); } } else { panic!("this request should return an `Err` variant") diff --git a/crates/matrix-sdk/tests/integration/refresh_token.rs b/crates/matrix-sdk/tests/integration/refresh_token.rs index a433388a5..eedc4cb6c 100644 --- a/crates/matrix-sdk/tests/integration/refresh_token.rs +++ b/crates/matrix-sdk/tests/integration/refresh_token.rs @@ -13,7 +13,6 @@ use matrix_sdk_test::{async_test, test_json}; use ruma::{ api::{ client::{account::register, error::ErrorKind, Error as ClientApiError}, - error::{FromHttpResponseError, ServerError}, MatrixVersion, }, assign, device_id, user_id, @@ -229,13 +228,11 @@ async fn refresh_token_not_handled() { .mount(&server) .await; - let res = client.whoami().await; + let res = client.whoami().await.unwrap_err(); assert_matches!( - res, - Err(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(ClientApiError { kind, .. }) - )))) if matches!(kind, ErrorKind::UnknownToken { .. }) - ) + res.as_ruma_api_error(), + Some(RumaApiError::ClientApi(ClientApiError { kind: ErrorKind::UnknownToken { .. }, .. })) + ); } #[async_test] @@ -362,12 +359,10 @@ async fn refresh_token_handled_failure() { .mount(&server) .await; - let res = client.whoami().await; + let res = client.whoami().await.unwrap_err(); assert_matches!( - res, - Err(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( - RumaApiError::ClientApi(ClientApiError { kind, .. }) - )))) if matches!(kind, ErrorKind::UnknownToken { .. }) + res.as_ruma_api_error(), + Some(RumaApiError::ClientApi(ClientApiError { kind: ErrorKind::UnknownToken { .. }, .. })) ) } diff --git a/examples/appservice_autojoin/src/main.rs b/examples/appservice_autojoin/src/main.rs index b1ceebc58..de95c0d3e 100644 --- a/examples/appservice_autojoin/src/main.rs +++ b/examples/appservice_autojoin/src/main.rs @@ -2,19 +2,15 @@ use std::env; use matrix_sdk_appservice::{ matrix_sdk::{ - self, event_handler::Ctx, room::Room, ruma::{ events::room::member::{MembershipState, OriginalSyncRoomMemberEvent}, UserId, }, - HttpError, RumaApiError, - }, - ruma::api::{ - client::{error::ErrorKind, uiaa::UiaaResponse}, - error::{FromHttpResponseError, ServerError}, + RumaApiError, }, + ruma::api::client::{error::ErrorKind, uiaa::UiaaResponse}, AppService, AppServiceBuilder, AppServiceRegistration, Result, }; use tracing::trace; @@ -40,15 +36,19 @@ pub async fn handle_room_member( } pub fn error_if_user_not_in_use(error: matrix_sdk_appservice::Error) -> Result<()> { - match error { + // FIXME: Use if-let chain once available + match &error { // If user is already in use that's OK. - matrix_sdk_appservice::Error::Matrix(matrix_sdk::Error::Http(HttpError::Api( - FromHttpResponseError::Server(ServerError::Known(RumaApiError::Uiaa( - UiaaResponse::MatrixError(error), - ))), - ))) if matches!(error.kind, ErrorKind::UserInUse) => Ok(()), - // In all other cases return with an error. - error => Err(error), + matrix_sdk_appservice::Error::Matrix(err) => match err.as_ruma_api_error() { + Some(RumaApiError::Uiaa(UiaaResponse::MatrixError(error))) + if matches!(error.kind, ErrorKind::UserInUse) => + { + Ok(()) + } + // In all other cases return with an error. + _ => Err(error), + }, + _ => Err(error), } }