use deadpool_sqlite::{CreatePoolError, PoolError};
#[cfg(feature = "event-cache")]
use matrix_sdk_base::event_cache_store::EventCacheStoreError;
#[cfg(feature = "state-store")]
use matrix_sdk_base::store::StoreError as StateStoreError;
#[cfg(feature = "crypto-store")]
use matrix_sdk_crypto::CryptoStoreError;
use thiserror::Error;
use tokio::io;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum OpenStoreError {
#[error("Failed to create the database's parent directory")]
CreateDir(#[source] io::Error),
#[error(transparent)]
CreatePool(#[from] CreatePoolError),
#[error("Failed to load database version")]
LoadVersion(#[source] rusqlite::Error),
#[error("Missing database version")]
MissingVersion,
#[error("Invalid database version")]
InvalidVersion,
#[error("Failed to run migrations")]
Migration(#[from] Error),
#[error(transparent)]
Pool(#[from] PoolError),
#[error("Failed to initialize the store cipher")]
InitCipher(#[from] matrix_sdk_store_encryption::Error),
#[error("Failed to load the store cipher from the DB")]
LoadCipher(#[source] rusqlite::Error),
#[error("Failed to save the store cipher to the DB")]
SaveCipher(#[source] rusqlite::Error),
}
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Sqlite(rusqlite::Error),
#[error("Failed to compute the maximum variable number from {0}")]
SqliteMaximumVariableNumber(i32),
#[error(transparent)]
Pool(PoolError),
#[error(transparent)]
Encode(rmp_serde::encode::Error),
#[error(transparent)]
Decode(rmp_serde::decode::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Encryption(matrix_sdk_store_encryption::Error),
#[error("can't save/load sessions or group sessions in the store before an account is stored")]
AccountUnset,
#[error(transparent)]
Pickle(#[from] vodozemac::PickleError),
#[error("An object failed to be decrypted while unpickling")]
Unpickle,
#[error("Redaction failed: {0}")]
Redaction(#[source] ruma::canonical_json::RedactionError),
}
macro_rules! impl_from {
( $ty:ty => $enum:ident::$variant:ident ) => {
impl From<$ty> for $enum {
fn from(value: $ty) -> Self {
Self::$variant(value)
}
}
};
}
impl_from!(rusqlite::Error => Error::Sqlite);
impl_from!(PoolError => Error::Pool);
impl_from!(rmp_serde::encode::Error => Error::Encode);
impl_from!(rmp_serde::decode::Error => Error::Decode);
impl_from!(matrix_sdk_store_encryption::Error => Error::Encryption);
#[cfg(feature = "crypto-store")]
impl From<Error> for CryptoStoreError {
fn from(e: Error) -> Self {
CryptoStoreError::backend(e)
}
}
#[cfg(feature = "state-store")]
impl From<Error> for StateStoreError {
fn from(e: Error) -> Self {
match e {
Error::Json(e) => StateStoreError::Json(e),
Error::Encryption(e) => StateStoreError::Encryption(e),
Error::Redaction(e) => StateStoreError::Redaction(e),
e => StateStoreError::backend(e),
}
}
}
#[cfg(feature = "event-cache")]
impl From<Error> for EventCacheStoreError {
fn from(e: Error) -> Self {
match e {
Error::Encryption(e) => EventCacheStoreError::Encryption(e),
e => EventCacheStoreError::backend(e),
}
}
}
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;