chore: rename error type and reexport error types

Closes #2
This commit is contained in:
Dmitriy Pleshevskiy 2021-10-21 23:56:39 +03:00
parent 829fecb7aa
commit 58269f5a64
8 changed files with 68 additions and 75 deletions

View File

@ -1,4 +1,3 @@
use ood_persistence::error::PersistenceError;
use std::error;
use std::fmt;
@ -8,7 +7,7 @@ pub type ApiResult<T> = Result<T, Error>;
#[derive(Debug)]
pub enum Error {
PersistenceError(PersistenceError),
PersistenceError(ood_persistence::Error),
Rest(RestKind),
Serde(serde_json::Error),
}
@ -25,8 +24,8 @@ impl fmt::Display for Error {
impl std::error::Error for Error {}
impl From<PersistenceError> for Error {
fn from(err: PersistenceError) -> Self {
impl From<ood_persistence::Error> for Error {
fn from(err: ood_persistence::Error) -> Self {
Self::PersistenceError(err)
}
}

View File

@ -14,7 +14,7 @@ pub trait PersistencePool: Send + Sync {
/// # Errors
///
/// Returns PersistenceError if pool cannot get a connection.
async fn get_connection(&self) -> error::Result<Self::Conn>;
async fn get_connection(&self) -> crate::Result<Self::Conn>;
}
/// Connection client knows about the inner connection, and also knows how to create transactions.
@ -40,7 +40,7 @@ pub trait ConnectionClient {
///
/// Returns PersistenceError if connection cannot update to transaction.
#[cfg(feature = "nightly")]
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>>;
async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>>;
}
/// Transaction client is updated connection client that can additionally commit and rollback data
@ -55,12 +55,12 @@ pub trait TransactionClient: ConnectionClient {
/// # Errors
///
/// Returns PersistenceError if transaction cannot commit
async fn commit(self) -> error::Result<()>;
async fn commit(self) -> crate::Result<()>;
/// Rolls the transaction back, discarding all changes made within it.
///
/// # Errors
///
/// Returns PersistenceError if transaction cannot rolls back.
async fn rollback(self) -> error::Result<()>;
async fn rollback(self) -> crate::Result<()>;
}

View File

@ -42,11 +42,11 @@ where
impl<'p> PersistencePool for NoTlsPersistence<'p> {
type Conn = NoTlsConnection<'p>;
async fn get_connection(&self) -> error::Result<Self::Conn> {
async fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0
.get()
.await
.map_err(|_| error::PersistenceError::GetConnection)
.map_err(|_| crate::Error::GetConnection)
.map(Connection)
}
}
@ -68,16 +68,16 @@ impl<'me> ConnectionClient for NoTlsConnection<'me> {
}
#[cfg(feature = "nightly")]
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0
.transaction()
.await
.map_err(|_| error::PersistenceError::UpgradeToTransaction)
.map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction)
}
}
/// Transaction wrap over tokio_postgres transaction.
/// Transaction wrap over `tokio_postgres` transaction.
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
#[cfg(feature = "nightly")]
@ -94,11 +94,11 @@ impl<'me> ConnectionClient for Transaction<'me> {
&mut self.0
}
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0
.transaction()
.await
.map_err(|_| error::PersistenceError::UpgradeToTransaction)
.map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction)
}
}
@ -106,17 +106,17 @@ impl<'me> ConnectionClient for Transaction<'me> {
#[cfg(feature = "nightly")]
#[async_trait]
impl<'me> TransactionClient for Transaction<'me> {
async fn commit(self) -> error::Result<()> {
async fn commit(self) -> crate::Result<()> {
self.0
.commit()
.await
.map_err(|_| error::PersistenceError::CommitTransaction)
.map_err(|_| crate::Error::CommitTransaction)
}
async fn rollback(self) -> error::Result<()> {
async fn rollback(self) -> crate::Result<()> {
self.0
.rollback()
.await
.map_err(|_| error::PersistenceError::RollbackTransaction)
.map_err(|_| crate::Error::RollbackTransaction)
}
}

View File

@ -1,4 +1,3 @@
use std::error;
use std::fmt;
#[cfg(feature = "bb8_postgres")]
@ -12,11 +11,11 @@ use r2d2_sqlite::rusqlite::Error as RusqliteError;
/// A helper type for any result with persistence error.
///
/// Use this type in your repository or in something else that implements methods for your persistence.
pub type Result<T> = std::result::Result<T, PersistenceError>;
pub type Result<T> = std::result::Result<T, Error>;
/// All supported kinds of persistence error
#[derive(Debug)]
pub enum PersistenceError {
pub enum Error {
/// Returns if we cannot get a connection from pool.
GetConnection,
/// Returns if we cannot upgrade connection to transaction.
@ -28,41 +27,37 @@ pub enum PersistenceError {
/// Returns if we cannot rolls back transaction.
#[cfg(feature = "nightly")]
RollbackTransaction,
/// Rest database errors contains here.
DbError(Box<dyn std::error::Error>),
/// Rest persistence errors contains here.
PersistenceError(Box<dyn std::error::Error>),
}
impl fmt::Display for PersistenceError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PersistenceError::GetConnection => f.write_str("Cannot get connection"),
Error::GetConnection => f.write_str("Cannot get connection"),
#[cfg(feature = "nightly")]
PersistenceError::UpgradeToTransaction => {
f.write_str("Cannot upgrade connection to transaction")
}
Error::UpgradeToTransaction => f.write_str("Cannot upgrade connection to transaction"),
#[cfg(feature = "nightly")]
PersistenceError::CommitTransaction => {
f.write_str("Cannot commit changes of transaction")
}
Error::CommitTransaction => f.write_str("Cannot commit changes of transaction"),
#[cfg(feature = "nightly")]
PersistenceError::RollbackTransaction => f.write_str("Cannot rolls transaction back"),
PersistenceError::DbError(err) => write!(f, "DbError: {}", err),
Error::RollbackTransaction => f.write_str("Cannot rolls transaction back"),
Error::PersistenceError(err) => write!(f, "DbError: {}", err),
}
}
}
impl error::Error for PersistenceError {}
impl std::error::Error for Error {}
#[cfg(any(feature = "r2d2_postgres", feature = "bb8_postgres"))]
impl From<PostgresError> for PersistenceError {
impl From<PostgresError> for Error {
fn from(err: PostgresError) -> Self {
Self::DbError(Box::new(err))
Self::PersistenceError(Box::new(err))
}
}
#[cfg(feature = "r2d2_sqlite")]
impl From<RusqliteError> for PersistenceError {
impl From<RusqliteError> for Error {
fn from(err: RusqliteError) -> Self {
Self::DbError(Box::new(err))
Self::PersistenceError(Box::new(err))
}
}

View File

@ -28,7 +28,7 @@
//! See examples directory.
//!
#![forbid(unsafe_code, non_ascii_idents)]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![warn(missing_docs)]
#![cfg_attr(feature = "nightly", feature(generic_associated_types))]
@ -87,3 +87,5 @@ pub mod r2d2_sqlite;
/// This module contains implementations for errors and result, that this
/// crate uses
pub mod error;
pub use error::{Error, Result};

View File

@ -1,4 +1,3 @@
use crate::error;
#[cfg(feature = "nightly")]
use crate::syn::TransactionClient;
use crate::syn::{ConnectionClient, PersistencePool};
@ -24,6 +23,7 @@ pub type NoTlsInnerConn = InnerConn<NoTlsManager>;
pub type NoTlsPool = Pool<NoTlsManager>;
/// It creates new persistence of r2d2 postgres implementation.
#[must_use]
pub fn new<M>(pool: &Pool<M>) -> Persistence<M>
where
M: r2d2::ManageConnection,
@ -40,10 +40,10 @@ where
impl PersistencePool for NoTlsPersistence<'_> {
type Conn = NoTlsConnection;
fn get_connection(&self) -> error::Result<Self::Conn> {
fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0
.get()
.map_err(|_| error::PersistenceError::GetConnection)
.map_err(|_| crate::Error::GetConnection)
.map(Connection)
}
}
@ -64,10 +64,10 @@ impl ConnectionClient for NoTlsConnection {
}
#[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0
.transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction)
.map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction)
}
}
@ -88,25 +88,23 @@ impl<'me> ConnectionClient for Transaction<'me> {
&mut self.0
}
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0
.transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction)
.map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction)
}
}
#[cfg(feature = "nightly")]
impl TransactionClient for Transaction<'_> {
fn commit(self) -> error::Result<()> {
self.0
.commit()
.map_err(|_| error::PersistenceError::CommitTransaction)
fn commit(self) -> crate::Result<()> {
self.0.commit().map_err(|_| crate::Error::CommitTransaction)
}
fn rollback(self) -> error::Result<()> {
fn rollback(self) -> crate::Result<()> {
self.0
.rollback()
.map_err(|_| error::PersistenceError::RollbackTransaction)
.map_err(|_| crate::Error::RollbackTransaction)
}
}

View File

@ -12,6 +12,7 @@ pub type InnerConn = PooledConnection<Manager>;
pub type InnerTrx<'t> = rusqlite::Transaction<'t>;
/// It creates new persistence of r2d2 sqlite implementation.
#[must_use]
pub fn new(pool: &Pool<Manager>) -> Persistence {
Persistence(pool)
}
@ -23,10 +24,10 @@ pub struct Persistence<'p>(&'p Pool<Manager>);
impl PersistencePool for Persistence<'_> {
type Conn = Connection;
fn get_connection(&self) -> error::Result<Self::Conn> {
fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0
.get()
.map_err(|_| error::PersistenceError::GetConnection)
.map_err(|_| crate::Error::GetConnection)
.map(Connection)
}
}
@ -45,10 +46,10 @@ impl ConnectionClient for Connection {
}
#[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0
.transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction)
.map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction)
}
}
@ -60,7 +61,7 @@ impl ConnectionClient for Connection {
/// # Limits
///
/// It doesn't support nested transaction, because the transaction in `rusqlite`
/// requires DerefMut, which cannot be implemented at the moment. 😣
/// requires `DerefMut`, which cannot be implemented at the moment. 😣
pub struct Transaction<'me>(InnerTrx<'me>);
impl<'me> ConnectionClient for Transaction<'me> {
@ -74,7 +75,7 @@ impl<'me> ConnectionClient for Transaction<'me> {
}
#[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> {
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
// At the moment we cannot implement nested transaction because
// the transaction in `rusqlite` requires DerefMut, which cannot be
// implemented yet 😣
@ -87,14 +88,12 @@ impl<'me> ConnectionClient for Transaction<'me> {
}
impl TransactionClient for Transaction<'_> {
fn commit(self) -> error::Result<()> {
self.0
.commit()
.map_err(|_| error::PersistenceError::CommitTransaction)
fn commit(self) -> crate::Result<()> {
self.0.commit().map_err(|_| crate::Error::CommitTransaction)
}
fn rollback(self) -> error::Result<()> {
fn rollback(self) -> crate::Result<()> {
self.0
.rollback()
.map_err(|_| error::PersistenceError::RollbackTransaction)
.map_err(|_| crate::Error::RollbackTransaction)
}
}

View File

@ -5,15 +5,15 @@ use crate::error;
/// Any implementation (database, file storage, memory or others) must be able to get a connection
/// to manipulate it afterwards.
pub trait PersistencePool {
/// ConnectionClient implementation that persistence can can return.
/// `ConnectionClient` implementation that persistence can can return.
type Conn: ConnectionClient;
/// Returns new connection.
///
/// # Errors
///
/// Returns PersistenceError if pool cannot get a connection.
fn get_connection(&self) -> error::Result<Self::Conn>;
/// Returns `PersistenceError` if pool cannot get a connection.
fn get_connection(&self) -> crate::Result<Self::Conn>;
}
/// Connection client knows about the inner connection, and also knows how to create transactions.
@ -21,7 +21,7 @@ pub trait ConnectionClient {
/// Inner connection
type InnerConn;
/// TransactionClient implementation in which the connection can be updated.
/// `TransactionClient` implementation in which the connection can be updated.
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
#[cfg(feature = "nightly")]
@ -36,9 +36,9 @@ pub trait ConnectionClient {
///
/// # Errors
///
/// Returns PersistenceError if connection cannot update to transaction.
/// Returns `PersistenceError` if connection cannot update to transaction.
#[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>>;
fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>>;
}
/// Transaction client is updated connection client that can additionally commit and rollback data
@ -51,13 +51,13 @@ pub trait TransactionClient: ConnectionClient {
///
/// # Errors
///
/// Returns PersistenceError if transaction cannot commit
fn commit(self) -> error::Result<()>;
/// Returns `PersistenceError` if transaction cannot commit
fn commit(self) -> crate::Result<()>;
/// Rolls the transaction back, discarding all changes made within it.
///
/// # Errors
///
/// Returns PersistenceError if transaction cannot rolls back.
fn rollback(self) -> error::Result<()>;
/// Returns `PersistenceError` if transaction cannot rolls back.
fn rollback(self) -> crate::Result<()>;
}