toasty_core/error/
connection_pool.rs

1use super::Error;
2
3/// Error from a connection pool.
4#[derive(Debug)]
5pub(super) struct ConnectionPool {
6    pub(super) inner: Box<dyn std::error::Error + Send + Sync>,
7}
8
9impl std::error::Error for ConnectionPool {
10    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
11        Some(self.inner.as_ref())
12    }
13}
14
15impl core::fmt::Display for ConnectionPool {
16    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
17        // Display the error and walk its source chain
18        core::fmt::Display::fmt(&self.inner, f)?;
19        let mut source = self.inner.source();
20        while let Some(err) = source {
21            write!(f, ": {}", err)?;
22            source = err.source();
23        }
24        Ok(())
25    }
26}
27
28impl Error {
29    /// Creates an error from a connection pool error.
30    ///
31    /// This is used for errors that occur when managing the connection pool (e.g., deadpool errors).
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// use toasty_core::Error;
37    ///
38    /// let io_err = std::io::Error::new(std::io::ErrorKind::TimedOut, "pool exhausted");
39    /// let err = Error::connection_pool(io_err);
40    /// assert!(err.is_connection_pool());
41    /// ```
42    pub fn connection_pool(err: impl std::error::Error + Send + Sync + 'static) -> Error {
43        Error::from(super::ErrorKind::ConnectionPool(ConnectionPool {
44            inner: Box::new(err),
45        }))
46    }
47
48    /// Returns `true` if this error is a connection pool error.
49    pub fn is_connection_pool(&self) -> bool {
50        matches!(self.kind(), super::ErrorKind::ConnectionPool(_))
51    }
52}