feat(core): add DatabaseError

This commit is contained in:
Ryan Leckey
2021-01-02 10:46:51 -08:00
parent 4d63978da1
commit 2195472e3e
4 changed files with 55 additions and 6 deletions

View File

@@ -2,6 +2,10 @@ use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
mod database;
pub use database::DatabaseError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
@@ -9,6 +13,8 @@ pub type Result<T> = std::result::Result<T, Error>;
pub enum Error {
Configuration { message: Cow<'static, str>, source: Option<Box<dyn StdError + Send + Sync>> },
Connect(Box<dyn DatabaseError>),
Network(std::io::Error),
}
@@ -30,14 +36,16 @@ impl Error {
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Network(source) => write!(f, "network: {}", source),
Self::Network(source) => write!(f, "{}", source),
Self::Connect(source) => write!(f, "{}", source),
Self::Configuration { message, source: None } => {
write!(f, "configuration: {}", message)
write!(f, "{}", message)
}
Self::Configuration { message, source: Some(source) } => {
write!(f, "configuration: {}: {}", message, source)
write!(f, "{}: {}", message, source)
}
}
}
@@ -57,12 +65,12 @@ impl StdError for Error {
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::Network(error)
Self::Network(error)
}
}
impl From<std::io::ErrorKind> for Error {
fn from(error: std::io::ErrorKind) -> Self {
Error::Network(error.into())
Self::Network(error.into())
}
}

View File

@@ -0,0 +1,13 @@
use std::error::Error as StdError;
/// `DatabaseError` is a trait representing an error that was returned from
/// the database.
///
/// Provides abstract access to information returned from the database about
/// the error.
///
#[allow(clippy::module_name_repetitions)]
pub trait DatabaseError: 'static + StdError + Send + Sync {
/// Returns the primary, human-readable error message.
fn message(&self) -> &str;
}

View File

@@ -41,7 +41,7 @@ pub mod blocking;
pub use connection::Connection;
pub use database::{Database, HasOutput};
pub use error::{Error, Result};
pub use error::{Error, Result, DatabaseError};
pub use options::ConnectOptions;
#[cfg(feature = "actix")]
pub use runtime::Actix;