diff --git a/matrix_sdk/examples/command_bot.rs b/matrix_sdk/examples/command_bot.rs index 43df2674..312964eb 100644 --- a/matrix_sdk/examples/command_bot.rs +++ b/matrix_sdk/examples/command_bot.rs @@ -73,7 +73,7 @@ async fn login_and_sync( .disable_ssl_verification() .state_store(Box::new(store)); - let homeserver_url = Url::parse(&homeserver_url)?; + let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); // create a new AsyncClient with the given homeserver url and config let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap(); diff --git a/matrix_sdk/examples/login.rs b/matrix_sdk/examples/login.rs index 724a7da3..3dd4d2ce 100644 --- a/matrix_sdk/examples/login.rs +++ b/matrix_sdk/examples/login.rs @@ -44,7 +44,7 @@ async fn login( let client_config = AsyncClientConfig::new() .proxy("http://localhost:8080")? .disable_ssl_verification(); - let homeserver_url = Url::parse(&homeserver_url)?; + let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap(); client.add_event_emitter(Box::new(EventCallback)).await; diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index dc17a949..1244a120 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -391,7 +391,7 @@ impl AsyncClient { /// # }); /// ``` pub async fn sync_with_state_store(&self) -> Result { - self.base_client.sync_with_state_store().await + Ok(self.base_client.sync_with_state_store().await?) } /// Login to the server. diff --git a/matrix_sdk/src/error.rs b/matrix_sdk/src/error.rs new file mode 100644 index 00000000..2670a989 --- /dev/null +++ b/matrix_sdk/src/error.rs @@ -0,0 +1,68 @@ +// Copyright 2020 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Error conditions. + +use reqwest::Error as ReqwestError; +use serde_json::Error as JsonError; +use thiserror::Error; + +use matrix_sdk_base::Error as MatrixError; + +use crate::api::Error as RumaClientError; +use crate::FromHttpResponseError as RumaResponseError; +use crate::IntoHttpError as RumaIntoHttpError; + +/// Result type of the rust-sdk. +pub type Result = std::result::Result; + +/// Internal representation of errors. +#[derive(Error, Debug)] +pub enum Error { + /// Queried endpoint requires authentication but was called on an anonymous client. + #[error("the queried endpoint requires authentication but was called before logging in")] + AuthenticationRequired, + + /// An error at the HTTP layer. + #[error(transparent)] + Reqwest(#[from] ReqwestError), + + /// An error de/serializing type for the `StateStore` + #[error(transparent)] + SerdeJson(#[from] JsonError), + + /// An error converting between ruma_client_api types and Hyper types. + #[error("can't parse the JSON response as a Matrix response")] + RumaResponse(RumaResponseError), + + /// An error converting between ruma_client_api types and Hyper types. + #[error("can't convert between ruma_client_api and hyper types.")] + IntoHttp(RumaIntoHttpError), + + /// An error occured in the Matrix client library. + #[error(transparent)] + MatrixError(#[from] MatrixError), +} + +impl From> for Error { + fn from(error: RumaResponseError) -> Self { + Self::RumaResponse(error) + } +} + +impl From for Error { + fn from(error: RumaIntoHttpError) -> Self { + Self::IntoHttp(error) + } +} diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index a300f268..30beba64 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -26,7 +26,7 @@ //! destroyed. #![deny(missing_docs)] -pub use matrix_sdk_base::{Error, EventEmitter, Result, Room, Session}; +pub use matrix_sdk_base::{EventEmitter, Room, Session}; pub use matrix_sdk_base::{JsonStore, RoomState, StateStore}; pub use matrix_sdk_common::*; pub use reqwest::header::InvalidHeaderValue; @@ -35,8 +35,10 @@ pub use reqwest::header::InvalidHeaderValue; pub use matrix_sdk_base::{Device, TrustState}; mod client; +mod error; mod request_builder; pub use client::{AsyncClient, AsyncClientConfig, SyncSettings}; +pub use error::{Error, Result}; pub use request_builder::{MessagesRequestBuilder, RoomBuilder}; pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/matrix_sdk_base/Cargo.toml b/matrix_sdk_base/Cargo.toml index 240210c0..a7f95bd9 100644 --- a/matrix_sdk_base/Cargo.toml +++ b/matrix_sdk_base/Cargo.toml @@ -17,15 +17,10 @@ encryption = ["matrix-sdk-crypto"] sqlite-cryptostore = ["matrix-sdk-crypto/sqlite-cryptostore"] [dependencies] -dirs = "2.0.2" futures = "0.3.4" -reqwest = "0.10.4" -http = "0.2.1" -url = "2.1.1" async-trait = "0.1.30" serde = "1.0.106" serde_json = "1.0.52" -uuid = { version = "0.8.1", features = ["v4"] } matrix-sdk-common = { path = "../matrix_sdk_common" } matrix-sdk-crypto = { path = "../matrix_sdk_crypto", optional = true } @@ -41,5 +36,7 @@ features = ["sync", "fs"] [dev-dependencies] matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } tokio = { version = "0.2.20", features = ["rt-threaded", "macros"] } +http = "0.2.1" +dirs = "2.0.2" tracing-subscriber = "0.2.5" tempfile = "3.1.0" diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index f0c4a2a5..621b0d80 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -302,8 +302,8 @@ impl Client { } pub(crate) async fn get_or_create_invited_room(&self, room_id: &RoomId) -> Arc> { - #[allow(clippy::or_fun_call)] let mut rooms = self.invited_rooms.write().await; + #[allow(clippy::or_fun_call)] rooms .entry(room_id.clone()) .or_insert(Arc::new(RwLock::new(Room::new( @@ -336,8 +336,8 @@ impl Client { } pub(crate) async fn get_or_create_left_room(&self, room_id: &RoomId) -> Arc> { - #[allow(clippy::or_fun_call)] let mut rooms = self.left_rooms.write().await; + #[allow(clippy::or_fun_call)] rooms .entry(room_id.clone()) .or_insert(Arc::new(RwLock::new(Room::new( @@ -620,17 +620,11 @@ impl Client { // event comes in e.g. move a joined room to a left room when leave event comes? // when events change state, updated signals to StateStore to update database - let mut updated = self.iter_joined_rooms(response).await?; + let updated_joined = self.iter_joined_rooms(response).await?; + let updated_invited = self.iter_invited_rooms(&response).await?; + let updated_left = self.iter_left_rooms(response).await?; - if self.iter_invited_rooms(&response).await? { - updated = true; - } - - if self.iter_left_rooms(response).await? { - updated = true; - } - - if updated { + if updated_joined || updated_invited || updated_left { let store = self.state_store.read().await; if let Some(store) = store.as_ref() { @@ -780,8 +774,8 @@ impl Client { } } - for mut event in &mut left_room.timeline.events { - if self.receive_left_timeline_event(room_id, &mut event).await { + for event in &mut left_room.timeline.events { + if self.receive_left_timeline_event(room_id, &event).await { updated = true; }; diff --git a/matrix_sdk_base/src/error.rs b/matrix_sdk_base/src/error.rs index 209667da..1cc24bb3 100644 --- a/matrix_sdk_base/src/error.rs +++ b/matrix_sdk_base/src/error.rs @@ -15,15 +15,9 @@ //! Error conditions. -use std::io::Error as IoError; - -use crate::api::Error as RumaClientError; -use crate::FromHttpResponseError as RumaResponseError; -use crate::IntoHttpError as RumaIntoHttpError; -use reqwest::Error as ReqwestError; use serde_json::Error as JsonError; +use std::io::Error as IoError; use thiserror::Error; -use url::ParseError; #[cfg(feature = "encryption")] use matrix_sdk_crypto::{MegolmError, OlmError}; @@ -37,41 +31,24 @@ pub enum Error { /// Queried endpoint requires authentication but was called on an anonymous client. #[error("the queried endpoint requires authentication but was called before logging in")] AuthenticationRequired, - /// An error at the HTTP layer. - #[error(transparent)] - Reqwest(#[from] ReqwestError), - /// An error when parsing a string as a URI. - #[error("can't parse the provided string as an URL")] - Uri(#[from] ParseError), - /// An error converting between ruma_client_api types and Hyper types. - #[error("can't parse the JSON response as a Matrix response")] - RumaResponse(RumaResponseError), - /// An error converting between ruma_client_api types and Hyper types. - #[error("can't convert between ruma_client_api and hyper types.")] - IntoHttp(RumaIntoHttpError), + /// An error de/serializing type for the `StateStore` #[error(transparent)] SerdeJson(#[from] JsonError), + /// An error de/serializing type for the `StateStore` #[error(transparent)] IoError(#[from] IoError), - #[cfg(feature = "encryption")] + /// An error occurred during a E2EE operation. + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] #[error(transparent)] OlmError(#[from] OlmError), + /// An error occurred during a E2EE group operation. + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] #[error(transparent)] MegolmError(#[from] MegolmError), } - -impl From> for Error { - fn from(error: RumaResponseError) -> Self { - Self::RumaResponse(error) - } -} - -impl From for Error { - fn from(error: RumaIntoHttpError) -> Self { - Self::IntoHttp(error) - } -} diff --git a/matrix_sdk_base/src/event_emitter/mod.rs b/matrix_sdk_base/src/event_emitter/mod.rs index 15d968ce..e3c49485 100644 --- a/matrix_sdk_base/src/event_emitter/mod.rs +++ b/matrix_sdk_base/src/event_emitter/mod.rs @@ -47,7 +47,6 @@ use crate::RoomState; /// # use std::ops::Deref; /// # use std::sync::Arc; /// # use std::{env, process::exit}; -/// # use url::Url; /// # use matrix_sdk_base::{ /// # self, /// # events::{ diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index 4d78b258..8ba5eb0c 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -28,7 +28,6 @@ pub use crate::{error::Error, error::Result, session::Session}; pub use matrix_sdk_common::*; -pub use reqwest::header::InvalidHeaderValue; mod client; mod error;