From 7f64580ce2ca147c47dcd0af9a60a345908ae19d Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 13 Jun 2024 17:16:59 +0200 Subject: [PATCH] sdk: use `Error::AuthenticationRequired` instead of `HttpError::AuthenticationRequired` The HttpError variant was misplaced, as it was always used when the `user_id()` is missing, which is causing `Error::AuthenticationRequired` errors everywhere else in the code base. This patch streamlines usage of `Error::AuthenticationRequired`, and gets rid of the `HttpError` variant. --- crates/matrix-sdk/src/account.rs | 11 ++++------- crates/matrix-sdk/src/error.rs | 5 ----- crates/matrix-sdk/src/room/mod.rs | 20 +++++++++----------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index 79504365d..12cdd5321 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -49,7 +49,7 @@ use ruma::{ use serde::Deserialize; use tracing::error; -use crate::{config::RequestConfig, Client, Error, HttpError, Result}; +use crate::{config::RequestConfig, Client, Error, Result}; /// A high-level API to manage the client owner's account. /// @@ -741,8 +741,7 @@ impl Account { &self, event_type: GlobalAccountDataEventType, ) -> Result>> { - let own_user = - self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let request = get_global_account_data::v3::Request::new(own_user.to_owned(), event_type); @@ -795,8 +794,7 @@ impl Account { where T: GlobalAccountDataEventContent, { - let own_user = - self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let request = set_global_account_data::v3::Request::new(own_user.to_owned(), &content)?; @@ -809,8 +807,7 @@ impl Account { event_type: GlobalAccountDataEventType, content: Raw, ) -> Result { - let own_user = - self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let request = set_global_account_data::v3::Request::new_raw(own_user.to_owned(), event_type, content); diff --git a/crates/matrix-sdk/src/error.rs b/crates/matrix-sdk/src/error.rs index c2c7fa63c..ee976341a 100644 --- a/crates/matrix-sdk/src/error.rs +++ b/crates/matrix-sdk/src/error.rs @@ -88,11 +88,6 @@ pub enum HttpError { #[error(transparent)] Reqwest(#[from] ReqwestError), - /// Queried endpoint requires authentication but was called on an anonymous - /// client. - #[error("the queried endpoint requires authentication but was called before logging in")] - AuthenticationRequired, - /// Queried endpoint is not meant for clients. #[error("the queried endpoint is not meant for clients")] NotClientRequest, diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 296f5fe08..5dabbe80b 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -104,7 +104,7 @@ use crate::{ room::power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt}, sync::RoomUpdate, utils::{IntoRawMessageLikeEventContent, IntoRawStateEventContent}, - BaseRoom, Client, Error, HttpError, HttpResult, Result, RoomState, TransmissionProgress, + BaseRoom, Client, Error, HttpResult, Result, RoomState, TransmissionProgress, }; pub mod futures; @@ -983,15 +983,15 @@ impl Room { &self, tag: TagName, tag_info: TagInfo, - ) -> HttpResult { - let user_id = self.client.user_id().ok_or(HttpError::AuthenticationRequired)?; + ) -> Result { + let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let request = create_tag::v3::Request::new( user_id.to_owned(), self.inner.room_id().to_owned(), tag.to_string(), tag_info, ); - self.client.send(request, None).await + Ok(self.client.send(request, None).await?) } /// Removes a tag from the room. @@ -1000,14 +1000,14 @@ impl Room { /// /// # Arguments /// * `tag` - The tag to remove. - pub async fn remove_tag(&self, tag: TagName) -> HttpResult { - let user_id = self.client.user_id().ok_or(HttpError::AuthenticationRequired)?; + pub async fn remove_tag(&self, tag: TagName) -> Result { + let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let request = delete_tag::v3::Request::new( user_id.to_owned(), self.inner.room_id().to_owned(), tag.to_string(), ); - self.client.send(request, None).await + Ok(self.client.send(request, None).await?) } /// Add or remove the `m.favourite` flag for this room. @@ -1071,8 +1071,7 @@ impl Room { /// # Arguments /// * `is_direct` - Whether to mark this room as direct. pub async fn set_is_direct(&self, is_direct: bool) -> Result<()> { - let user_id = - self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let mut content = self .client @@ -2602,8 +2601,7 @@ impl Room { /// Set a flag on the room to indicate that the user has explicitly marked /// it as (un)read. pub async fn set_unread_flag(&self, unread: bool) -> Result<()> { - let user_id = - self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; let content = MarkedUnreadEventContent::new(unread);