toasty_core/error/
transaction_timeout.rs

1use std::time::Duration;
2
3use crate::{Error, error::ErrorKind};
4
5/// Error when a transaction exceeds its configured timeout.
6///
7/// The transaction is automatically rolled back when this occurs.
8#[derive(Debug)]
9pub(super) struct TransactionTimeout {
10    duration: Duration,
11}
12
13impl Error {
14    /// Creates a transaction timeout error.
15    ///
16    /// Returned when the transaction closure exceeds the configured timeout.
17    /// The transaction is automatically rolled back.
18    ///
19    /// # Examples
20    ///
21    /// ```
22    /// use std::time::Duration;
23    /// use toasty_core::Error;
24    ///
25    /// let err = Error::transaction_timeout(Duration::from_secs(30));
26    /// assert!(err.is_transaction_timeout());
27    /// ```
28    pub fn transaction_timeout(duration: Duration) -> Error {
29        ErrorKind::TransactionTimeout(TransactionTimeout { duration }).into()
30    }
31
32    /// Returns `true` if this error is a transaction timeout.
33    pub fn is_transaction_timeout(&self) -> bool {
34        matches!(self.kind(), ErrorKind::TransactionTimeout(_))
35    }
36}
37
38impl std::error::Error for TransactionTimeout {}
39
40impl core::fmt::Display for TransactionTimeout {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "Transaction timed out after {:?}", self.duration)
43    }
44}