Skip to content

Commit edb5d41

Browse files
zecakehpoljar
authored andcommitted
refactor(sdk): Make RumaApiError a type alias of UiaaResponse
They have similar variants containing the same data so it's not useful to have separate types. Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 6a551f3 commit edb5d41

4 files changed

Lines changed: 27 additions & 56 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[**breaking**] `RumaApiError` is now a type alias for `UiaaResponse`, because they have similar
2+
variants containing the same data. The `ClientApi` variant is now `MatrixError`, and the `Uiaa`
3+
variant is `AuthResponse`.

crates/matrix-sdk/src/authentication/oauth/qrcode/rendezvous_channel/msc_4108.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn get_header(
5555
fn response_to_error(status: StatusCode, body: Vec<u8>) -> HttpError {
5656
match http::Response::builder().status(status).body(body).map_err(IntoHttpError::from) {
5757
Ok(response) => {
58-
let error = FromHttpResponseError::<RumaApiError>::Server(RumaApiError::ClientApi(
58+
let error = FromHttpResponseError::<RumaApiError>::Server(RumaApiError::MatrixError(
5959
ruma::api::error::Error::from_http_response(response),
6060
));
6161

crates/matrix-sdk/src/error.rs

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,7 @@ pub type HttpResult<T> = std::result::Result<T, HttpError>;
5959

6060
/// An error response from a Matrix API call, using a client API specific
6161
/// representation if the endpoint is from that.
62-
#[derive(Error, Debug)]
63-
pub enum RumaApiError {
64-
/// A client API response error.
65-
#[error(transparent)]
66-
ClientApi(ruma::api::error::Error),
67-
68-
/// A user-interactive authentication API error.
69-
///
70-
/// When registering or authenticating, the Matrix server can send a
71-
/// `UiaaInfo` as the error type, this is a User-Interactive Authentication
72-
/// API response. This represents an error with information about how to
73-
/// authenticate the user.
74-
#[error("User-Interactive Authentication required.")]
75-
Uiaa(UiaaInfo),
76-
}
77-
78-
impl RumaApiError {
79-
/// If `self` is `ClientApi(e)`, returns `Some(e)`.
80-
///
81-
/// Otherwise, returns `None`.
82-
pub fn as_client_api_error(&self) -> Option<&ruma::api::error::Error> {
83-
as_variant!(self, Self::ClientApi)
84-
}
85-
}
62+
pub type RumaApiError = UiaaResponse;
8663

8764
/// An HTTP error, representing either a connection error or an error while
8865
/// converting the raw HTTP response into a Matrix response.
@@ -134,35 +111,35 @@ impl HttpError {
134111
_ => None
135112
}
136113
}
137-
138-
/// Shorthand for
139-
/// <code>.[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error])</code>.
140-
pub fn as_client_api_error(&self) -> Option<&ruma::api::error::Error> {
141-
self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error)
142-
}
143114
}
144115

145116
// Another impl block that's formatted with rustfmt.
146117
impl HttpError {
147118
/// If `self` is a server error in the `errcode` + `error` format expected
148-
/// for client-API endpoints, returns the error kind (`errcode`).
119+
/// for client API endpoints, returns it.
120+
pub fn as_client_api_error(&self) -> Option<&ruma::api::error::Error> {
121+
self.as_ruma_api_error().and_then(as_variant!(UiaaResponse::MatrixError))
122+
}
123+
124+
/// If `self` is a server error in the `errcode` + `error` format expected
125+
/// for client API endpoints, returns the error kind (`errcode`).
149126
pub fn client_api_error_kind(&self) -> Option<&ErrorKind> {
150127
self.as_client_api_error().and_then(ruma::api::error::Error::error_kind)
151128
}
152129

153-
/// Try to destructure the error into an universal interactive auth info.
130+
/// Try to destructure the error into a user-interactive auth info.
154131
///
155-
/// Some requests require universal interactive auth, doing such a request
132+
/// Some requests require user-interactive auth, doing such a request
156133
/// will always fail the first time with a 401 status code, the response
157-
/// body will contain info how the client can authenticate.
134+
/// body will contain info on how the client can authenticate.
158135
///
159136
/// The request will need to be retried, this time containing additional
160137
/// authentication data.
161138
///
162-
/// This method is an convenience method to get to the info the server
139+
/// This method is a convenience method to get to the info the server
163140
/// returned on the first, failed request.
164141
pub fn as_uiaa_response(&self) -> Option<&UiaaInfo> {
165-
self.as_ruma_api_error().and_then(as_variant!(RumaApiError::Uiaa))
142+
self.as_ruma_api_error().and_then(as_variant!(UiaaResponse::AuthResponse))
166143
}
167144

168145
/// Returns whether an HTTP error response should be qualified as transient
@@ -221,16 +198,16 @@ impl RetryKind {
221198
/// format defined in the [spec].
222199
///
223200
/// [spec]: https://spec.matrix.org/v1.11/client-server-api/#standard-error-response
224-
fn from_api_error(api_error: &RumaApiError) -> Self {
201+
fn from_api_error(api_error: &UiaaResponse) -> Self {
225202
match api_error {
226-
RumaApiError::ClientApi(client_error) => match client_error.error_kind() {
203+
UiaaResponse::MatrixError(client_error) => match client_error.error_kind() {
227204
Some(ErrorKind::LimitExceeded(limit_exceeded)) => {
228205
RetryKind::from_retry_after(limit_exceeded.retry_after.as_ref())
229206
}
230207
Some(ErrorKind::Unrecognized) => RetryKind::Permanent,
231208
_ => RetryKind::from_status_code(client_error.status_code),
232209
},
233-
RumaApiError::Uiaa(_) => RetryKind::Permanent,
210+
UiaaResponse::AuthResponse(_) => RetryKind::Permanent,
234211
}
235212
}
236213

@@ -440,14 +417,14 @@ impl Error {
440417
as_variant!(self, Self::Http).and_then(|e| e.as_ruma_api_error())
441418
}
442419

443-
/// Shorthand for
444-
/// <code>.[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error])</code>.
420+
/// If `self` is a server error in the `errcode` + `error` format expected
421+
/// for client API endpoints, returns it.
445422
pub fn as_client_api_error(&self) -> Option<&ruma::api::error::Error> {
446-
self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error)
423+
self.as_ruma_api_error().and_then(as_variant!(UiaaResponse::MatrixError))
447424
}
448425

449426
/// If `self` is a server error in the `errcode` + `error` format expected
450-
/// for client-API endpoints, returns the error kind (`errcode`).
427+
/// for client API endpoints, returns the error kind (`errcode`).
451428
pub fn client_api_error_kind(&self) -> Option<&ErrorKind> {
452429
self.as_client_api_error().and_then(ruma::api::error::Error::error_kind)
453430
}
@@ -464,7 +441,7 @@ impl Error {
464441
/// This method is an convenience method to get to the info the server
465442
/// returned on the first, failed request.
466443
pub fn as_uiaa_response(&self) -> Option<&UiaaInfo> {
467-
self.as_ruma_api_error().and_then(as_variant!(RumaApiError::Uiaa))
444+
self.as_ruma_api_error().and_then(as_variant!(UiaaResponse::AuthResponse))
468445
}
469446
}
470447

@@ -586,16 +563,7 @@ pub enum RoomKeyImportError {
586563

587564
impl From<FromHttpResponseError<ruma::api::error::Error>> for HttpError {
588565
fn from(err: FromHttpResponseError<ruma::api::error::Error>) -> Self {
589-
Self::Api(Box::new(err.map(RumaApiError::ClientApi)))
590-
}
591-
}
592-
593-
impl From<FromHttpResponseError<UiaaResponse>> for HttpError {
594-
fn from(err: FromHttpResponseError<UiaaResponse>) -> Self {
595-
Self::Api(Box::new(err.map(|e| match e {
596-
UiaaResponse::AuthResponse(i) => RumaApiError::Uiaa(i),
597-
UiaaResponse::MatrixError(e) => RumaApiError::ClientApi(e),
598-
})))
566+
Self::Api(Box::new(err.map(Into::into)))
599567
}
600568
}
601569

crates/matrix-sdk/src/widget/machine/from_widget.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl FromWidgetErrorResponse {
7171
let message = error.to_string();
7272
let matrix_api_error = match error {
7373
HttpError::Api(error) => {
74-
as_variant!(*error, ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err)) => err)
74+
as_variant!(*error, ruma::api::error::FromHttpResponseError::Server(RumaApiError::MatrixError(err)) => err)
7575
}
7676
_ => None,
7777
};

0 commit comments

Comments
 (0)