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