Improve Datastore and Transaction errors

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-06 21:57:12 +01:00
parent 4d072a7f6c
commit 2239e4becf
5 changed files with 44 additions and 32 deletions

View file

@ -28,13 +28,13 @@ pub enum Error {
#[error("Conditional clause is not truthy")] #[error("Conditional clause is not truthy")]
Ignore, Ignore,
/// There was an error when connecting to the underlying datastore /// There was a problem with the underlying datastore
#[error("Couldn't setup connection to underlying datastore")] #[error("There was a problem with the underlying datastore: {0}")]
Ds, Ds(String),
/// There was an error when starting a new transaction /// There was a problem with a datastore transaction
#[error("Couldn't create a database transaction")] #[error("There was a problem with a datastore transaction: {0}")]
Tx, Tx(String),
/// The transaction was already cancelled or committed /// The transaction was already cancelled or committed
#[error("Couldn't update a finished transaction")] #[error("Couldn't update a finished transaction")]
@ -235,27 +235,33 @@ pub enum Error {
#[error("Key decoding error: {0}")] #[error("Key decoding error: {0}")]
Decode(#[from] DecodeError), 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 /// Represents an underlying error with Tokio message channels
#[cfg(feature = "parallel")] #[cfg(feature = "parallel")]
#[error("Tokio Error: {0}")] #[error("Tokio Error: {0}")]
Tokio(#[from] TokioError<(Option<Thing>, Value)>), Tokio(#[from] TokioError<(Option<Thing>, Value)>),
} }
#[cfg(feature = "kv-echodb")]
impl From<EchoDBError> for Error {
fn from(e: EchoDBError) -> Error {
Error::Tx(e.to_string())
}
}
#[cfg(feature = "kv-indxdb")]
impl From<IndxDBError> for Error {
fn from(e: IndxDBError) -> Error {
Error::Tx(e.to_string())
}
}
#[cfg(feature = "kv-tikv")]
impl From<TiKVError> for Error {
fn from(e: TiKVError) -> Error {
Error::Tx(e.to_string())
}
}
impl Serialize for Error { impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where

View file

@ -33,7 +33,7 @@ impl Datastore {
rw: write, rw: write,
tx, tx,
}), }),
Err(_) => Err(Error::Tx), Err(e) => Err(Error::Tx(e.to_string())),
} }
} }
} }

View file

@ -21,9 +21,12 @@ pub struct Transaction {
impl Datastore { impl Datastore {
// Open a new database // Open a new database
pub async fn new(path: &str) -> Result<Datastore, Error> { pub async fn new(path: &str) -> Result<Datastore, Error> {
Ok(Datastore { match indxdb::db::new(path).await {
db: indxdb::db::new(path).await?, Ok(db) => Ok(Datastore {
}) db,
}),
Err(e) => Err(Error::Ds(e.to_string())),
}
} }
// Start a new transaction // Start a new transaction
pub async fn transaction(&self, write: bool, _: bool) -> Result<Transaction, Error> { pub async fn transaction(&self, write: bool, _: bool) -> Result<Transaction, Error> {
@ -33,7 +36,7 @@ impl Datastore {
rw: write, rw: write,
tx, tx,
}), }),
Err(_) => Err(Error::Tx), Err(e) => Err(Error::Tx(e.to_string())),
} }
} }
} }

View file

@ -33,7 +33,7 @@ impl Datastore {
rw: write, rw: write,
tx, tx,
}), }),
Err(_) => Err(Error::Tx), Err(e) => Err(Error::Tx(e.to_string())),
} }
} }
} }

View file

@ -21,9 +21,12 @@ pub struct Transaction {
impl Datastore { impl Datastore {
// Open a new database // Open a new database
pub async fn new(path: &str) -> Result<Datastore, Error> { pub async fn new(path: &str) -> Result<Datastore, Error> {
Ok(Datastore { match tikv::TransactionClient::new(vec![path]).await {
db: tikv::TransactionClient::new(vec![path]).await?, Ok(db) => Ok(Datastore {
}) db,
}),
Err(e) => Err(Error::Ds(e.to_string())),
}
} }
// Start a new transaction // Start a new transaction
pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> { pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> {
@ -34,7 +37,7 @@ impl Datastore {
rw: write, rw: write,
tx, tx,
}), }),
Err(_) => Err(Error::Tx), Err(e) => Err(Error::Tx(e.to_string())),
}, },
false => match self.db.begin_pessimistic().await { false => match self.db.begin_pessimistic().await {
Ok(tx) => Ok(Transaction { Ok(tx) => Ok(Transaction {
@ -42,7 +45,7 @@ impl Datastore {
rw: write, rw: write,
tx, tx,
}), }),
Err(_) => Err(Error::Tx), Err(e) => Err(Error::Tx(e.to_string())),
}, },
} }
} }