toasty_core/error/driver_operation_failed.rs
1use super::Error;
2
3/// Error when a database driver operation fails.
4///
5/// This wraps errors from underlying database driver libraries when operations fail:
6/// - Connection errors (rusqlite, tokio-postgres, AWS SDK)
7/// - Query execution errors
8/// - Transaction operation errors (BEGIN, COMMIT, ROLLBACK)
9/// - Schema operation errors (CREATE TABLE, CREATE INDEX)
10/// - URL parsing errors for connection strings
11#[derive(Debug)]
12pub(super) struct DriverOperationFailed {
13 pub(super) inner: Box<dyn std::error::Error + Send + Sync>,
14}
15
16impl std::error::Error for DriverOperationFailed {
17 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
18 Some(self.inner.as_ref())
19 }
20}
21
22impl core::fmt::Display for DriverOperationFailed {
23 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
24 // Display the error and walk its source chain
25 core::fmt::Display::fmt(&self.inner, f)?;
26 let mut source = self.inner.source();
27 while let Some(err) = source {
28 write!(f, ": {}", err)?;
29 source = err.source();
30 }
31 Ok(())
32 }
33}
34
35impl Error {
36 /// Creates an error from a driver operation failure.
37 ///
38 /// This is the preferred way to convert driver-specific errors (rusqlite, tokio-postgres,
39 /// mysql_async, AWS SDK errors, etc.) into toasty errors.
40 ///
41 /// # Examples
42 ///
43 /// ```
44 /// use toasty_core::Error;
45 ///
46 /// let io_err = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "refused");
47 /// let err = Error::driver_operation_failed(io_err);
48 /// assert!(err.is_driver_operation_failed());
49 /// ```
50 pub fn driver_operation_failed(err: impl std::error::Error + Send + Sync + 'static) -> Error {
51 Error::from(super::ErrorKind::DriverOperationFailed(
52 DriverOperationFailed {
53 inner: Box::new(err),
54 },
55 ))
56 }
57
58 /// Returns `true` if this error is a driver operation failure.
59 pub fn is_driver_operation_failed(&self) -> bool {
60 matches!(self.kind(), super::ErrorKind::DriverOperationFailed(_))
61 }
62}