Skip to content

Commit 7f64580

Browse files
committed
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.
1 parent 08d7bd0 commit 7f64580

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

crates/matrix-sdk/src/account.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use ruma::{
4949
use serde::Deserialize;
5050
use tracing::error;
5151

52-
use crate::{config::RequestConfig, Client, Error, HttpError, Result};
52+
use crate::{config::RequestConfig, Client, Error, Result};
5353

5454
/// A high-level API to manage the client owner's account.
5555
///
@@ -741,8 +741,7 @@ impl Account {
741741
&self,
742742
event_type: GlobalAccountDataEventType,
743743
) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>> {
744-
let own_user =
745-
self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?;
744+
let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
746745

747746
let request = get_global_account_data::v3::Request::new(own_user.to_owned(), event_type);
748747

@@ -795,8 +794,7 @@ impl Account {
795794
where
796795
T: GlobalAccountDataEventContent,
797796
{
798-
let own_user =
799-
self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?;
797+
let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
800798

801799
let request = set_global_account_data::v3::Request::new(own_user.to_owned(), &content)?;
802800

@@ -809,8 +807,7 @@ impl Account {
809807
event_type: GlobalAccountDataEventType,
810808
content: Raw<AnyGlobalAccountDataEventContent>,
811809
) -> Result<set_global_account_data::v3::Response> {
812-
let own_user =
813-
self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?;
810+
let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
814811

815812
let request =
816813
set_global_account_data::v3::Request::new_raw(own_user.to_owned(), event_type, content);

crates/matrix-sdk/src/error.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ pub enum HttpError {
8888
#[error(transparent)]
8989
Reqwest(#[from] ReqwestError),
9090

91-
/// Queried endpoint requires authentication but was called on an anonymous
92-
/// client.
93-
#[error("the queried endpoint requires authentication but was called before logging in")]
94-
AuthenticationRequired,
95-
9691
/// Queried endpoint is not meant for clients.
9792
#[error("the queried endpoint is not meant for clients")]
9893
NotClientRequest,

crates/matrix-sdk/src/room/mod.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ use crate::{
104104
room::power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt},
105105
sync::RoomUpdate,
106106
utils::{IntoRawMessageLikeEventContent, IntoRawStateEventContent},
107-
BaseRoom, Client, Error, HttpError, HttpResult, Result, RoomState, TransmissionProgress,
107+
BaseRoom, Client, Error, HttpResult, Result, RoomState, TransmissionProgress,
108108
};
109109

110110
pub mod futures;
@@ -983,15 +983,15 @@ impl Room {
983983
&self,
984984
tag: TagName,
985985
tag_info: TagInfo,
986-
) -> HttpResult<create_tag::v3::Response> {
987-
let user_id = self.client.user_id().ok_or(HttpError::AuthenticationRequired)?;
986+
) -> Result<create_tag::v3::Response> {
987+
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
988988
let request = create_tag::v3::Request::new(
989989
user_id.to_owned(),
990990
self.inner.room_id().to_owned(),
991991
tag.to_string(),
992992
tag_info,
993993
);
994-
self.client.send(request, None).await
994+
Ok(self.client.send(request, None).await?)
995995
}
996996

997997
/// Removes a tag from the room.
@@ -1000,14 +1000,14 @@ impl Room {
10001000
///
10011001
/// # Arguments
10021002
/// * `tag` - The tag to remove.
1003-
pub async fn remove_tag(&self, tag: TagName) -> HttpResult<delete_tag::v3::Response> {
1004-
let user_id = self.client.user_id().ok_or(HttpError::AuthenticationRequired)?;
1003+
pub async fn remove_tag(&self, tag: TagName) -> Result<delete_tag::v3::Response> {
1004+
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
10051005
let request = delete_tag::v3::Request::new(
10061006
user_id.to_owned(),
10071007
self.inner.room_id().to_owned(),
10081008
tag.to_string(),
10091009
);
1010-
self.client.send(request, None).await
1010+
Ok(self.client.send(request, None).await?)
10111011
}
10121012

10131013
/// Add or remove the `m.favourite` flag for this room.
@@ -1071,8 +1071,7 @@ impl Room {
10711071
/// # Arguments
10721072
/// * `is_direct` - Whether to mark this room as direct.
10731073
pub async fn set_is_direct(&self, is_direct: bool) -> Result<()> {
1074-
let user_id =
1075-
self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?;
1074+
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
10761075

10771076
let mut content = self
10781077
.client
@@ -2602,8 +2601,7 @@ impl Room {
26022601
/// Set a flag on the room to indicate that the user has explicitly marked
26032602
/// it as (un)read.
26042603
pub async fn set_unread_flag(&self, unread: bool) -> Result<()> {
2605-
let user_id =
2606-
self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?;
2604+
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
26072605

26082606
let content = MarkedUnreadEventContent::new(unread);
26092607

0 commit comments

Comments
 (0)