rocket_db_pools_community/
error.rs

1use std::fmt;
2
3/// A general error type for use by [`Pool`](crate::Pool#implementing)
4/// implementors and returned by the [`Connection`](crate::Connection) request
5/// guard.
6#[allow(clippy::large_enum_variant)]
7#[derive(Debug)]
8pub enum Error<A, B = A> {
9    /// An error that occurred during database/pool initialization.
10    Init(A),
11
12    /// An error that occurred while retrieving a connection from the pool.
13    Get(B),
14
15    /// A [`Figment`](crate::figment::Figment) configuration error.
16    Config(crate::figment::Error),
17}
18
19impl<A: fmt::Display, B: fmt::Display> fmt::Display for Error<A, B> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Error::Init(e) => write!(f, "failed to initialize database: {}", e),
23            Error::Get(e) => write!(f, "failed to get db connection: {}", e),
24            Error::Config(e) => write!(f, "bad configuration: {}", e),
25        }
26    }
27}
28
29impl<A, B> std::error::Error for Error<A, B>
30where
31    A: fmt::Debug + fmt::Display,
32    B: fmt::Debug + fmt::Display,
33{
34}
35
36impl<A, B> From<crate::figment::Error> for Error<A, B> {
37    fn from(e: crate::figment::Error) -> Self {
38        Self::Config(e)
39    }
40}