Skip to content

Commit

Permalink
feat(base)!: Use boxed errors in StoreError and CryptoStoreError
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKirb committed May 6, 2022
1 parent 8b805f9 commit be2a818
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 73 deletions.
7 changes: 3 additions & 4 deletions crates/matrix-crypto-ffi/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
ops::Deref,
};

use anyhow::anyhow;
use base64::{decode_config, encode, STANDARD_NO_PAD};
use js_int::UInt;
use matrix_sdk_common::deserialized_responses::AlgorithmInfo;
Expand Down Expand Up @@ -102,9 +101,9 @@ impl OlmMachine {
// variant for the state store. Not sure what to do about
// this.
matrix_sdk_sled::OpenStoreError::Crypto(r) => r.into(),
matrix_sdk_sled::OpenStoreError::Sled(s) => {
CryptoStoreError::CryptoStore(anyhow!(s).into())
}
matrix_sdk_sled::OpenStoreError::Sled(s) => CryptoStoreError::CryptoStore(
matrix_sdk_crypto::store::CryptoStoreError::Backend(Box::new(s)),
),
_ => unreachable!(),
}
})?,
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ experimental-timeline = []
testing = ["http"]

[dependencies]
anyhow = "1.0.57"
async-stream = "0.3.3"
async-trait = "0.1.53"
chacha20poly1305 = { version = "0.9.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub use self::memory_store::MemoryStore;
pub enum StoreError {
#[error(transparent)]
/// An error happened in the underlying database backend.
Backend(#[from] anyhow::Error),
Backend(#[from] Box<dyn std::error::Error + Send + Sync>),
/// An error happened while serializing or deserializing some data.
#[error(transparent)]
Json(#[from] serde_json::Error),
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ testing = ["http"]
[dependencies]
aes = "0.8.1"
aes-gcm = "0.9.4"
anyhow = "1.0.57"
atomic = "0.5.1"
async-trait = "0.1.53"
base64 = "0.13.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ pub enum CryptoStoreError {

/// A problem with the underlying database backend
#[error(transparent)]
Backend(#[from] anyhow::Error),
Backend(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// Trait abstracting a store that the `OlmMachine` uses to store cryptographic
Expand Down
11 changes: 5 additions & 6 deletions crates/matrix-sdk-indexeddb/src/cryptostore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::{
sync::{Arc, RwLock},
};

use anyhow::anyhow;
use async_trait::async_trait;
use dashmap::DashSet;
use indexed_db_futures::prelude::*;
Expand Down Expand Up @@ -121,7 +120,7 @@ impl From<IndexeddbStoreError> for CryptoStoreError {
match frm {
IndexeddbStoreError::Json(e) => CryptoStoreError::Serialization(e),
IndexeddbStoreError::CryptoStoreError(e) => e,
_ => CryptoStoreError::Backend(anyhow!(frm)),
_ => CryptoStoreError::Backend(Box::new(frm)),
}
}
}
Expand Down Expand Up @@ -218,9 +217,9 @@ impl IndexeddbStore {
Some(key) => StoreCipher::import(passphrase, &key)
.map_err(|_| CryptoStoreError::UnpicklingError)?,
None => {
let key = StoreCipher::new().map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
let key = StoreCipher::new().map_err(|e| CryptoStoreError::Backend(Box::new(e)))?;
let encrypted =
key.export(passphrase).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
key.export(passphrase).map_err(|e| CryptoStoreError::Backend(Box::new(e)))?;

let tx: IdbTransaction<'_> = db.transaction_on_one_with_mode(
"matrix-sdk-crypto",
Expand Down Expand Up @@ -248,7 +247,7 @@ impl IndexeddbStore {
fn serialize_value(&self, value: &impl Serialize) -> Result<JsValue, CryptoStoreError> {
if let Some(key) = &self.store_cipher {
let value =
key.encrypt_value(value).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?;
key.encrypt_value(value).map_err(|e| CryptoStoreError::Backend(Box::new(e)))?;

Ok(JsValue::from_serde(&value)?)
} else {
Expand All @@ -262,7 +261,7 @@ impl IndexeddbStore {
) -> Result<T, CryptoStoreError> {
if let Some(key) = &self.store_cipher {
let value: Vec<u8> = value.into_serde()?;
key.decrypt_value(&value).map_err(|e| CryptoStoreError::Backend(anyhow!(e)))
key.decrypt_value(&value).map_err(|e| CryptoStoreError::Backend(Box::new(e)))
} else {
Ok(value.into_serde()?)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-indexeddb/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl From<SerializationError> for StoreError {
expected, found
)),
},
_ => StoreError::Backend(anyhow!(e)),
_ => StoreError::Backend(Box::new(e)),
}
}
}
Expand Down Expand Up @@ -307,7 +307,7 @@ impl IndexeddbStore {
Some(ref cipher) => key.encode_to_range_secure(table_name, cipher),
None => key.encode_to_range(),
}
.map_err(|e| SerializationError::StoreError(StoreError::Backend(anyhow!(e))))
.map_err(|e| SerializationError::StoreError(StoreError::Backend(anyhow!(e).into())))
}

#[cfg(feature = "experimental-timeline")]
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk-sled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ crypto-store = [
experimental-timeline = ["matrix-sdk-base/experimental-timeline"]

[dependencies]
anyhow = "1.0.57"
async-stream = "0.3.3"
async-trait = "0.1.53"
dashmap = "5.2.0"
Expand Down
Loading

0 comments on commit be2a818

Please sign in to comment.