doc: add for errors

This commit is contained in:
Dmitriy Pleshevskiy 2021-10-17 23:25:22 +03:00
parent 4f28aa01a2
commit 5e3e813900

View file

@ -1,14 +1,26 @@
use std::error; use std::error;
use std::fmt; use std::fmt;
/// 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, PersistenceError>;
/// All supported kinds of persistence error
#[derive(Debug)] #[derive(Debug)]
pub enum PersistenceError { pub enum PersistenceError {
/// Returns if we cannot get a connection from pool.
GetConnection, GetConnection,
/// Returns if we cannot upgrade connection to transaction.
#[cfg(feature = "nightly")]
UpgradeToTransaction, UpgradeToTransaction,
/// Returns if we cannot commit transaction.
#[cfg(feature = "nightly")]
CommitTransaction, CommitTransaction,
/// Returns if we cannot rolls back transaction.
#[cfg(feature = "nightly")]
RollbackTransaction, RollbackTransaction,
/// Rest database errors contains here.
DbError(Box<dyn std::error::Error>), DbError(Box<dyn std::error::Error>),
} }
@ -16,11 +28,16 @@ impl fmt::Display for PersistenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
PersistenceError::GetConnection => f.write_str("Cannot get connection"), PersistenceError::GetConnection => f.write_str("Cannot get connection"),
#[cfg(feature = "nightly")]
PersistenceError::UpgradeToTransaction => { PersistenceError::UpgradeToTransaction => {
f.write_str("Cannot upgrade connection to transaction") f.write_str("Cannot upgrade connection to transaction")
} }
PersistenceError::CommitTransaction => f.write_str("Cannot commit transaction"), #[cfg(feature = "nightly")]
PersistenceError::RollbackTransaction => f.write_str("Cannot rollback transaction"), PersistenceError::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), PersistenceError::DbError(err) => write!(f, "DbError: {}", err),
} }
} }