diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index 754dbb19e..9d6111d8f 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -27,7 +27,7 @@ use matrix_sdk_base::{Error as SdkBaseError, StoreError}; use reqwest::Error as ReqwestError; use ruma::{ api::{ - client::uiaa::{UiaaInfo, UiaaResponse as UiaaError}, + client::uiaa::{UiaaInfo, UiaaResponse}, error::{FromHttpResponseError, IntoHttpError, ServerError}, }, events::tag::InvalidUserTagName, @@ -51,6 +51,15 @@ pub enum RumaApiError { #[error(transparent)] ClientApi(ruma::api::client::Error), + /// A user-interactive authentication API error. + /// + /// When registering or authenticating, the Matrix server can send a + /// `UiaaResponse` as the error type, this is a User-Interactive + /// Authentication API response. This represents an error with + /// information about how to authenticate the user. + #[error(transparent)] + Uiaa(UiaaResponse), + /// Another API response error. #[error(transparent)] Other(ruma::api::error::MatrixError), @@ -81,15 +90,6 @@ pub enum HttpError { #[error(transparent)] IntoHttp(#[from] IntoHttpError), - /// An error occurred while authenticating. - /// - /// When registering or authenticating the Matrix server can send a - /// `UiaaResponse` as the error type, this is a User-Interactive - /// Authentication API response. This represents an error with - /// information about how to authenticate the user. - #[error(transparent)] - UiaaError(#[from] FromHttpResponseError), - /// The server returned a status code that should be retried. #[error("Server returned an error {0}")] Server(StatusCode), @@ -229,8 +229,8 @@ 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::UiaaError(FromHttpResponseError::Server(ServerError::Known( - UiaaError::AuthResponse(i), + if let HttpError::Api(FromHttpResponseError::Server(ServerError::Known( + RumaApiError::Uiaa(UiaaResponse::AuthResponse(i)), ))) = self { Some(i) @@ -253,9 +253,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::UiaaError(FromHttpResponseError::Server( - ServerError::Known(UiaaError::AuthResponse(i)), - ))) = self + if let Error::Http(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( + RumaApiError::Uiaa(UiaaResponse::AuthResponse(i)), + )))) = self { Some(i) } else { @@ -270,6 +270,12 @@ impl From> for HttpError { } } +impl From> for HttpError { + fn from(err: FromHttpResponseError) -> Self { + Self::Api(err.map(|e| e.map(RumaApiError::Uiaa))) + } +} + impl From> for HttpError { fn from(err: FromHttpResponseError) -> Self { Self::Api(err.map(|e| e.map(RumaApiError::Other))) diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index e6fd39606..8286b9aef 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -228,8 +228,12 @@ async fn register_error() { }); if let Err(err) = client.register(user).await { - if let HttpError::UiaaError(FromHttpResponseError::Server(ServerError::Known( - UiaaResponse::MatrixError(client_api::Error { kind, message, status_code }), + if let HttpError::Api(FromHttpResponseError::Server(ServerError::Known( + RumaApiError::Uiaa(UiaaResponse::MatrixError(client_api::Error { + kind, + message, + status_code, + })), ))) = err { if let client_api::error::ErrorKind::Forbidden = kind { diff --git a/examples/appservice_autojoin/src/main.rs b/examples/appservice_autojoin/src/main.rs index a68ab3a73..b1ceebc58 100644 --- a/examples/appservice_autojoin/src/main.rs +++ b/examples/appservice_autojoin/src/main.rs @@ -9,7 +9,7 @@ use matrix_sdk_appservice::{ events::room::member::{MembershipState, OriginalSyncRoomMemberEvent}, UserId, }, - HttpError, + HttpError, RumaApiError, }, ruma::api::{ client::{error::ErrorKind, uiaa::UiaaResponse}, @@ -42,8 +42,10 @@ pub async fn handle_room_member( pub fn error_if_user_not_in_use(error: matrix_sdk_appservice::Error) -> Result<()> { match error { // If user is already in use that's OK. - matrix_sdk_appservice::Error::Matrix(matrix_sdk::Error::Http(HttpError::UiaaError( - FromHttpResponseError::Server(ServerError::Known(UiaaResponse::MatrixError(error))), + 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),