mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-04-26 02:01:00 -04:00
refactor: Make use of as_ruma_api_error internally
This commit is contained in:
committed by
Jonas Platte
parent
23c5929cfa
commit
d18b57dcdd
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 { .. }, .. }))
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user