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")]
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<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 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where

View file

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

View file

@ -21,9 +21,12 @@ pub struct Transaction {
impl Datastore {
// Open a new database
pub async fn new(path: &str) -> Result<Datastore, Error> {
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<Transaction, Error> {
@ -33,7 +36,7 @@ impl Datastore {
rw: write,
tx,
}),
Err(_) => Err(Error::Tx),
Err(e) => Err(Error::Tx(e.to_string())),
}
}
}

View file

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

View file

@ -21,9 +21,12 @@ pub struct Transaction {
impl Datastore {
// Open a new database
pub async fn new(path: &str) -> Result<Datastore, Error> {
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<Transaction, Error> {
@ -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())),
},
}
}