rocket_db_pools/
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#[derive(Debug)]
7pub enum Error<A, B = A> {
8    /// An error that occurred during database/pool initialization.
9    Init(A),
10
11    /// An error that occurred while retrieving a connection from the pool.
12    Get(B),
13
14    /// A [`Figment`](crate::figment::Figment) configuration error.
15    Config(crate::figment::Error),
16}
17
18impl<A: fmt::Display, B: fmt::Display> fmt::Display for Error<A, B> {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Error::Init(e) => write!(f, "failed to initialize database: {}", e),
22            Error::Get(e) => write!(f, "failed to get db connection: {}", e),
23            Error::Config(e) => write!(f, "bad configuration: {}", e),
24        }
25    }
26}
27
28impl<A, B> std::error::Error for Error<A, B>
29    where A: fmt::Debug + fmt::Display, B: fmt::Debug + fmt::Display {}
30
31impl<A, B> From<crate::figment::Error> for Error<A, B> {
32    fn from(e: crate::figment::Error) -> Self {
33        Self::Config(e)
34    }
35}