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