matrix-sdk: Split out the error types in our base and matrix-sdk crates.

This commit is contained in:
Damir Jelić 2020-05-08 12:39:36 +02:00
parent 9461f08906
commit 8a7aefac3b
10 changed files with 93 additions and 57 deletions

View file

@ -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();

View file

@ -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;

View file

@ -391,7 +391,7 @@ impl AsyncClient {
/// # });
/// ```
pub async fn sync_with_state_store(&self) -> Result<bool> {
self.base_client.sync_with_state_store().await
Ok(self.base_client.sync_with_state_store().await?)
}
/// Login to the server.

68
matrix_sdk/src/error.rs Normal file
View file

@ -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<T> = std::result::Result<T, Error>;
/// 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<RumaClientError>),
/// 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<RumaResponseError<RumaClientError>> for Error {
fn from(error: RumaResponseError<RumaClientError>) -> Self {
Self::RumaResponse(error)
}
}
impl From<RumaIntoHttpError> for Error {
fn from(error: RumaIntoHttpError) -> Self {
Self::IntoHttp(error)
}
}

View file

@ -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");

View file

@ -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"

View file

@ -302,8 +302,8 @@ impl Client {
}
pub(crate) async fn get_or_create_invited_room(&self, room_id: &RoomId) -> Arc<RwLock<Room>> {
#[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<RwLock<Room>> {
#[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;
};

View file

@ -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<RumaClientError>),
/// 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<RumaResponseError<RumaClientError>> for Error {
fn from(error: RumaResponseError<RumaClientError>) -> Self {
Self::RumaResponse(error)
}
}
impl From<RumaIntoHttpError> for Error {
fn from(error: RumaIntoHttpError) -> Self {
Self::IntoHttp(error)
}
}

View file

@ -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::{

View file

@ -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;