feat(sdk): Add {RumaApiError,HttpError,Error}::as_client_api_error

This commit is contained in:
Jonas Platte
2022-11-03 15:30:32 +01:00
committed by Jonas Platte
parent 5b25b8967c
commit 85ea9554e5

View File

@@ -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 `<code>` 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
/// <code>.[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error])</code>.
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<dyn std::error::Error + Send + Sync>),
}
#[rustfmt::skip] // stop rustfmt breaking the `<code>` 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
/// <code>.[as_ruma_api_error](Self::as_ruma_api_error)().[and_then](Option::and_then)([RumaApiError::as_client_api_error])</code>.
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.