store: Add a store specific error type.
parent
5db9eadd89
commit
f517ac6f37
|
@ -1,14 +1,37 @@
|
||||||
use std::io::Result;
|
use std::io::Error as IoError;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use url::ParseError;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use thiserror::Error;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use super::olm::Account;
|
use super::olm::Account;
|
||||||
|
use olm_rs::errors::OlmAccountError;
|
||||||
|
|
||||||
#[cfg(feature = "sqlite-cryptostore")]
|
#[cfg(feature = "sqlite-cryptostore")]
|
||||||
pub mod sqlite;
|
pub mod sqlite;
|
||||||
|
|
||||||
|
#[cfg(feature = "sqlite-cryptostore")]
|
||||||
|
use sqlx::Error as SqlxError;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum CryptoStoreError {
|
||||||
|
#[error("can't read or write from the store")]
|
||||||
|
Io(#[from] IoError),
|
||||||
|
#[error("Olm operation failed")]
|
||||||
|
OlmAccountError(#[from] OlmAccountError),
|
||||||
|
#[error("URL can't be parsed")]
|
||||||
|
UrlParse(#[from] ParseError),
|
||||||
|
// TODO flatten the SqlxError to make it easier for other store
|
||||||
|
// implementations.
|
||||||
|
#[cfg(feature = "sqlite-cryptostore")]
|
||||||
|
#[error("database error")]
|
||||||
|
DatabaseError(#[from] SqlxError),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, CryptoStoreError>;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CryptoStore {
|
pub trait CryptoStore {
|
||||||
async fn load_account(&self) -> Result<Account>;
|
async fn load_account(&self) -> Result<Account>;
|
||||||
|
|
|
@ -46,11 +46,9 @@ impl SqliteStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_to_url<P: AsRef<Path>>(path: P) -> Result<Url> {
|
fn path_to_url<P: AsRef<Path>>(path: P) -> Result<Url> {
|
||||||
let url = Url::from_directory_path(path.as_ref()).expect("Can't create URL from directory");
|
// TODO this returns an empty error if the path isn't absolute.
|
||||||
let url = url
|
let url = Url::from_directory_path(path.as_ref()).expect("Invalid path");
|
||||||
.join(DATABASE_NAME)
|
Ok(url.join(DATABASE_NAME)?)
|
||||||
.expect("Can't append database name to URL");
|
|
||||||
Ok(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn open_helper(
|
async fn open_helper(
|
||||||
|
@ -85,8 +83,7 @@ impl SqliteStore {
|
||||||
);
|
);
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -113,10 +110,13 @@ impl CryptoStore for SqliteStore {
|
||||||
.bind(&*self.user_id)
|
.bind(&*self.user_id)
|
||||||
.bind(&*self.device_id)
|
.bind(&*self.device_id)
|
||||||
.fetch_one(&mut *connection)
|
.fetch_one(&mut *connection)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(Account::from_pickle(pickle, self.get_pickle_mode(), shared).unwrap())
|
Ok(Account::from_pickle(
|
||||||
|
pickle,
|
||||||
|
self.get_pickle_mode(),
|
||||||
|
shared,
|
||||||
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()> {
|
async fn save_account(&self, account: Arc<Mutex<Account>>) -> Result<()> {
|
||||||
|
@ -134,8 +134,7 @@ impl CryptoStore for SqliteStore {
|
||||||
.bind(&pickle)
|
.bind(&pickle)
|
||||||
.bind(acc.shared)
|
.bind(acc.shared)
|
||||||
.execute(&mut *connection)
|
.execute(&mut *connection)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
query(
|
query(
|
||||||
"UPDATE account
|
"UPDATE account
|
||||||
|
@ -149,8 +148,7 @@ impl CryptoStore for SqliteStore {
|
||||||
.bind(&*self.user_id)
|
.bind(&*self.user_id)
|
||||||
.bind(&*self.device_id)
|
.bind(&*self.device_id)
|
||||||
.execute(&mut *connection)
|
.execute(&mut *connection)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue