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
53
54
55
56
57
58
59
60
61
62
use super::Error;
/// Error when a database driver operation fails.
///
/// This wraps errors from underlying database driver libraries when operations fail:
/// - Connection errors (rusqlite, tokio-postgres, AWS SDK)
/// - Query execution errors
/// - Transaction operation errors (BEGIN, COMMIT, ROLLBACK)
/// - Schema operation errors (CREATE TABLE, CREATE INDEX)
/// - URL parsing errors for connection strings
#[derive(Debug)]
pub(super) struct DriverOperationFailed {
pub(super) inner: Box<dyn std::error::Error + Send + Sync>,
}
impl std::error::Error for DriverOperationFailed {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.inner.as_ref())
}
}
impl core::fmt::Display for DriverOperationFailed {
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 driver operation failure.
///
/// This is the preferred way to convert driver-specific errors (rusqlite, tokio-postgres,
/// mysql_async, AWS SDK errors, etc.) into toasty errors.
///
/// # Examples
///
/// ```
/// use toasty_core::Error;
///
/// let io_err = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "refused");
/// let err = Error::driver_operation_failed(io_err);
/// assert!(err.is_driver_operation_failed());
/// ```
pub fn driver_operation_failed(err: impl std::error::Error + Send + Sync + 'static) -> Error {
Error::from(super::ErrorKind::DriverOperationFailed(
DriverOperationFailed {
inner: Box::new(err),
},
))
}
/// Returns `true` if this error is a driver operation failure.
pub fn is_driver_operation_failed(&self) -> bool {
matches!(self.kind(), super::ErrorKind::DriverOperationFailed(_))
}
}