1use std::fmt;
18
19pub type Result<T> = std::result::Result<T, Error>;
21
22#[derive(Debug, Clone, thiserror::Error)]
24pub enum ConfigurationError {
25 #[error("max_connections must be greater than 0, got {0}")]
27 InvalidMaxConnections(usize),
28
29 #[error("database_url is invalid: {0}")]
31 InvalidDatabaseUrl(String),
32}
33
34#[derive(Debug, thiserror::Error)]
36pub enum Error {
37 #[error("Database error: {0}")]
39 Database(String),
40
41 #[error("Connection pool error: {0}")]
43 ConnectionPool(String),
44
45 #[error("Configuration error: {0}")]
47 Configuration(#[from] ConfigurationError),
48
49 #[error("Invalid prefix: {0}")]
51 InvalidPrefix(String),
52
53 #[error("Invalid URI: {0}")]
55 InvalidUri(String),
56}
57
58impl Error {
59 pub fn database(msg: impl fmt::Display) -> Self {
61 Self::Database(msg.to_string())
62 }
63
64 pub fn connection_pool(msg: impl fmt::Display) -> Self {
66 Self::ConnectionPool(msg.to_string())
67 }
68
69 pub fn invalid_prefix(msg: impl fmt::Display) -> Self {
71 Self::InvalidPrefix(msg.to_string())
72 }
73
74 pub fn invalid_uri(msg: impl fmt::Display) -> Self {
76 Self::InvalidUri(msg.to_string())
77 }
78}
79
80impl From<tokio_postgres::Error> for Error {
81 fn from(e: tokio_postgres::Error) -> Self {
82 Self::Database(e.to_string())
83 }
84}
85
86impl From<deadpool_postgres::PoolError> for Error {
87 fn from(e: deadpool_postgres::PoolError) -> Self {
88 Self::ConnectionPool(e.to_string())
89 }
90}
91
92impl From<deadpool_postgres::BuildError> for Error {
93 fn from(e: deadpool_postgres::BuildError) -> Self {
94 Self::ConnectionPool(format!("Pool build error: {}", e))
95 }
96}
97
98impl From<deadpool_postgres::CreatePoolError> for Error {
99 fn from(e: deadpool_postgres::CreatePoolError) -> Self {
100 Self::ConnectionPool(format!("Pool creation error: {}", e))
101 }
102}