use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, thiserror::Error)]
pub enum ConfigurationError {
#[error("max_connections must be greater than 0, got {0}")]
InvalidMaxConnections(usize),
#[error("database_url is invalid: {0}")]
InvalidDatabaseUrl(String),
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Database error: {0}")]
Database(String),
#[error("Connection pool error: {0}")]
ConnectionPool(String),
#[error("Configuration error: {0}")]
Configuration(#[from] ConfigurationError),
#[error("Invalid prefix: {0}")]
InvalidPrefix(String),
#[error("Invalid URI: {0}")]
InvalidUri(String),
}
impl Error {
pub fn database(msg: impl fmt::Display) -> Self {
Self::Database(msg.to_string())
}
pub fn connection_pool(msg: impl fmt::Display) -> Self {
Self::ConnectionPool(msg.to_string())
}
pub fn invalid_prefix(msg: impl fmt::Display) -> Self {
Self::InvalidPrefix(msg.to_string())
}
pub fn invalid_uri(msg: impl fmt::Display) -> Self {
Self::InvalidUri(msg.to_string())
}
}
impl From<tokio_postgres::Error> for Error {
fn from(e: tokio_postgres::Error) -> Self {
Self::Database(e.to_string())
}
}
impl From<deadpool_postgres::PoolError> for Error {
fn from(e: deadpool_postgres::PoolError) -> Self {
Self::ConnectionPool(e.to_string())
}
}
impl From<deadpool_postgres::BuildError> for Error {
fn from(e: deadpool_postgres::BuildError) -> Self {
Self::ConnectionPool(format!("Pool build error: {}", e))
}
}
impl From<deadpool_postgres::CreatePoolError> for Error {
fn from(e: deadpool_postgres::CreatePoolError) -> Self {
Self::ConnectionPool(format!("Pool creation error: {}", e))
}
}