refactor(sdk): Reduce size of HttpError.

This patch reduces the size of `HttpError` from 160 bytes to 24 bytes.
This commit is contained in:
Ivan Enderlin
2025-05-16 12:13:50 +02:00
parent 29a8556f10
commit e41dbd6300
3 changed files with 29 additions and 11 deletions

View File

@@ -174,7 +174,7 @@ async fn discover_homeserver(
)
.await
.map_err(|e| match e {
HttpError::Api(err) => ClientBuildError::AutoDiscovery(err),
HttpError::Api(err) => ClientBuildError::AutoDiscovery(*err),
err => ClientBuildError::Http(err),
})?;

View File

@@ -100,8 +100,9 @@ pub enum HttpError {
NotClientRequest,
/// API response error (deserialization, or a Matrix-specific error).
// `Box` its inner value to reduce the enum size.
#[error(transparent)]
Api(#[from] FromHttpResponseError<RumaApiError>),
Api(#[from] Box<FromHttpResponseError<RumaApiError>>),
/// Error when creating an API request (e.g. serialization of
/// body/headers/query parameters).
@@ -121,7 +122,12 @@ impl HttpError {
///
/// Otherwise, returns `None`.
pub fn as_ruma_api_error(&self) -> Option<&RumaApiError> {
as_variant!(self, Self::Api(FromHttpResponseError::Server(e)) => e)
match self {
Self::Api(error) => {
as_variant!(error.as_ref(), FromHttpResponseError::Server)
},
_ => None
}
}
/// Shorthand for
@@ -163,14 +169,21 @@ impl HttpError {
// internet, or that the remote is, so retry a few times.
HttpError::Reqwest(_) => RetryKind::NetworkFailure,
HttpError::Api(FromHttpResponseError::Server(api_error)) => {
RetryKind::from_api_error(api_error)
}
HttpError::Api(error) => match error.as_ref() {
FromHttpResponseError::Server(api_error) => RetryKind::from_api_error(api_error),
_ => RetryKind::Permanent,
},
_ => RetryKind::Permanent,
}
}
}
impl From<FromHttpResponseError<RumaApiError>> for HttpError {
fn from(value: FromHttpResponseError<RumaApiError>) -> Self {
Self::Api(Box::new(value))
}
}
/// How should we behave with respect to retry behavior after an [`HttpError`]
/// happened?
pub(crate) enum RetryKind {
@@ -537,22 +550,22 @@ pub enum RoomKeyImportError {
impl From<FromHttpResponseError<ruma::api::client::Error>> for HttpError {
fn from(err: FromHttpResponseError<ruma::api::client::Error>) -> Self {
Self::Api(err.map(RumaApiError::ClientApi))
Self::Api(Box::new(err.map(RumaApiError::ClientApi)))
}
}
impl From<FromHttpResponseError<UiaaResponse>> for HttpError {
fn from(err: FromHttpResponseError<UiaaResponse>) -> Self {
Self::Api(err.map(|e| match e {
Self::Api(Box::new(err.map(|e| match e {
UiaaResponse::AuthResponse(i) => RumaApiError::Uiaa(i),
UiaaResponse::MatrixError(e) => RumaApiError::ClientApi(e),
}))
})))
}
}
impl From<FromHttpResponseError<ruma::api::error::MatrixError>> for HttpError {
fn from(err: FromHttpResponseError<ruma::api::error::MatrixError>) -> Self {
Self::Api(err.map(RumaApiError::Other))
Self::Api(Box::new(err.map(RumaApiError::Other)))
}
}

View File

@@ -52,7 +52,12 @@ impl FromWidgetErrorResponse {
/// Create a error response to send to the widget from an http error.
pub(crate) fn from_http_error(error: HttpError) -> Self {
let message = error.to_string();
let matrix_api_error = as_variant!(error, HttpError::Api(ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err))) => err);
let matrix_api_error = match error {
HttpError::Api(error) => {
as_variant!(*error, ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err)) => err)
}
_ => None,
};
Self {
error: FromWidgetError {