From 85ea9554e5b41e12b965293ac61fa1d4c9d54387 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 3 Nov 2022 15:30:32 +0100 Subject: [PATCH] feat(sdk): Add {RumaApiError,HttpError,Error}::as_client_api_error --- crates/matrix-sdk/src/error.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index c1dde2b20..328c88cb3 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -65,6 +65,19 @@ pub enum RumaApiError { Other(ruma::api::error::MatrixError), } +impl RumaApiError { + /// If `self` is `ClientApi(e)` or `Uiaa(MatrixError(e))`, returns + /// `Some(e)`. + /// + /// Otherwise, returns `None`. + pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> { + match self { + Self::ClientApi(e) | Self::Uiaa(UiaaResponse::MatrixError(e)) => Some(e), + _ => None, + } + } +} + /// An HTTP error, representing either a connection error or an error while /// converting the raw HTTP response into a Matrix response. #[derive(Error, Debug)] @@ -103,6 +116,7 @@ pub enum HttpError { RefreshToken(#[from] RefreshTokenError), } +#[rustfmt::skip] // stop rustfmt breaking the `` in docs across multiple lines impl HttpError { /// If `self` is `Api(Server(Known(e)))`, returns `Some(e)`. /// @@ -113,6 +127,12 @@ impl HttpError { _ => None, } } + + /// Shorthand for + /// .[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error]). + pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> { + self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error) + } } /// Internal representation of errors. @@ -205,6 +225,7 @@ pub enum Error { UnknownError(Box), } +#[rustfmt::skip] // stop rustfmt breaking the `` in docs across multiple lines impl Error { /// If `self` is `Http(Api(Server(Known(e))))`, returns `Some(e)`. /// @@ -215,6 +236,12 @@ impl Error { _ => None, } } + + /// Shorthand for + /// .[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error]). + pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> { + self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error) + } } /// Error for the room key importing functionality.