From 2239e4becf614ff9c2f88bc284a60d5df6c2cb72 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 6 May 2022 21:57:12 +0100 Subject: [PATCH] Improve Datastore and Transaction errors --- lib/src/err/mod.rs | 48 +++++++++++++++++++++++------------------ lib/src/kvs/file/mod.rs | 2 +- lib/src/kvs/ixdb/mod.rs | 11 ++++++---- lib/src/kvs/mem/mod.rs | 2 +- lib/src/kvs/tikv/mod.rs | 13 ++++++----- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index c2521599..33d4fa0f 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -28,13 +28,13 @@ pub enum Error { #[error("Conditional clause is not truthy")] Ignore, - /// There was an error when connecting to the underlying datastore - #[error("Couldn't setup connection to underlying datastore")] - Ds, + /// There was a problem with the underlying datastore + #[error("There was a problem with the underlying datastore: {0}")] + Ds(String), - /// There was an error when starting a new transaction - #[error("Couldn't create a database transaction")] - Tx, + /// There was a problem with a datastore transaction + #[error("There was a problem with a datastore transaction: {0}")] + Tx(String), /// The transaction was already cancelled or committed #[error("Couldn't update a finished transaction")] @@ -235,27 +235,33 @@ pub enum Error { #[error("Key decoding error: {0}")] Decode(#[from] DecodeError), - /// Represents an underlying error from the EchoDB instance - #[cfg(feature = "kv-echodb")] - #[error("Datastore error: {0}")] - EchoDB(#[from] EchoDBError), - - /// Represents an underlying error from the IndxDB instance - #[cfg(feature = "kv-indxdb")] - #[error("Datastore error: {0}")] - IndxDB(#[from] IndxDBError), - - /// Represents an underlying error from the TiKV instance - #[cfg(feature = "kv-tikv")] - #[error("Datastore error: {0}")] - TiKV(#[from] TiKVError), - /// Represents an underlying error with Tokio message channels #[cfg(feature = "parallel")] #[error("Tokio Error: {0}")] Tokio(#[from] TokioError<(Option, Value)>), } +#[cfg(feature = "kv-echodb")] +impl From for Error { + fn from(e: EchoDBError) -> Error { + Error::Tx(e.to_string()) + } +} + +#[cfg(feature = "kv-indxdb")] +impl From for Error { + fn from(e: IndxDBError) -> Error { + Error::Tx(e.to_string()) + } +} + +#[cfg(feature = "kv-tikv")] +impl From for Error { + fn from(e: TiKVError) -> Error { + Error::Tx(e.to_string()) + } +} + impl Serialize for Error { fn serialize(&self, serializer: S) -> Result where diff --git a/lib/src/kvs/file/mod.rs b/lib/src/kvs/file/mod.rs index 31613f69..28e2a707 100644 --- a/lib/src/kvs/file/mod.rs +++ b/lib/src/kvs/file/mod.rs @@ -33,7 +33,7 @@ impl Datastore { rw: write, tx, }), - Err(_) => Err(Error::Tx), + Err(e) => Err(Error::Tx(e.to_string())), } } } diff --git a/lib/src/kvs/ixdb/mod.rs b/lib/src/kvs/ixdb/mod.rs index 675b5805..ef55b8ea 100644 --- a/lib/src/kvs/ixdb/mod.rs +++ b/lib/src/kvs/ixdb/mod.rs @@ -21,9 +21,12 @@ pub struct Transaction { impl Datastore { // Open a new database pub async fn new(path: &str) -> Result { - Ok(Datastore { - db: indxdb::db::new(path).await?, - }) + match indxdb::db::new(path).await { + Ok(db) => Ok(Datastore { + db, + }), + Err(e) => Err(Error::Ds(e.to_string())), + } } // Start a new transaction pub async fn transaction(&self, write: bool, _: bool) -> Result { @@ -33,7 +36,7 @@ impl Datastore { rw: write, tx, }), - Err(_) => Err(Error::Tx), + Err(e) => Err(Error::Tx(e.to_string())), } } } diff --git a/lib/src/kvs/mem/mod.rs b/lib/src/kvs/mem/mod.rs index 0d5ca476..2f010f63 100644 --- a/lib/src/kvs/mem/mod.rs +++ b/lib/src/kvs/mem/mod.rs @@ -33,7 +33,7 @@ impl Datastore { rw: write, tx, }), - Err(_) => Err(Error::Tx), + Err(e) => Err(Error::Tx(e.to_string())), } } } diff --git a/lib/src/kvs/tikv/mod.rs b/lib/src/kvs/tikv/mod.rs index d9721604..c887c0be 100644 --- a/lib/src/kvs/tikv/mod.rs +++ b/lib/src/kvs/tikv/mod.rs @@ -21,9 +21,12 @@ pub struct Transaction { impl Datastore { // Open a new database pub async fn new(path: &str) -> Result { - Ok(Datastore { - db: tikv::TransactionClient::new(vec![path]).await?, - }) + match tikv::TransactionClient::new(vec![path]).await { + Ok(db) => Ok(Datastore { + db, + }), + Err(e) => Err(Error::Ds(e.to_string())), + } } // Start a new transaction pub async fn transaction(&self, write: bool, lock: bool) -> Result { @@ -34,7 +37,7 @@ impl Datastore { rw: write, tx, }), - Err(_) => Err(Error::Tx), + Err(e) => Err(Error::Tx(e.to_string())), }, false => match self.db.begin_pessimistic().await { Ok(tx) => Ok(Transaction { @@ -42,7 +45,7 @@ impl Datastore { rw: write, tx, }), - Err(_) => Err(Error::Tx), + Err(e) => Err(Error::Tx(e.to_string())), }, } }