feat(sdk)!: Merge HttpError::UiaaError into HttpError::Api

This commit is contained in:
Jonas Platte
2022-10-11 19:56:48 +02:00
committed by Jonas Platte
parent f07735291e
commit f4e0cab11f
3 changed files with 32 additions and 20 deletions

View File

@@ -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<UiaaError>),
/// 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<FromHttpResponseError<ruma::api::client::Error>> for HttpError {
}
}
impl From<FromHttpResponseError<UiaaResponse>> for HttpError {
fn from(err: FromHttpResponseError<UiaaResponse>) -> Self {
Self::Api(err.map(|e| e.map(RumaApiError::Uiaa)))
}
}
impl From<FromHttpResponseError<ruma::api::error::MatrixError>> for HttpError {
fn from(err: FromHttpResponseError<ruma::api::error::MatrixError>) -> Self {
Self::Api(err.map(|e| e.map(RumaApiError::Other)))

View File

@@ -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 {

View File

@@ -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),