toasty_core/error/read_only_transaction.rs
1use super::Error;
2
3/// Error when a write operation is attempted inside a read-only transaction.
4///
5/// Drivers should classify the backend's read-only-write-rejected
6/// errors here: PostgreSQL SQLSTATE `25006`, MySQL error `1792`, and
7/// equivalents. User code can branch on
8/// [`is_read_only_transaction`](Error::is_read_only_transaction).
9#[derive(Debug)]
10pub(super) struct ReadOnlyTransaction {
11 message: Box<str>,
12}
13
14impl std::error::Error for ReadOnlyTransaction {}
15
16impl core::fmt::Display for ReadOnlyTransaction {
17 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
18 write!(f, "read-only transaction: {}", self.message)
19 }
20}
21
22impl Error {
23 /// Creates a read-only transaction error.
24 ///
25 /// Returned when a write operation is attempted inside a read-only
26 /// transaction (e.g. PostgreSQL SQLSTATE 25006, MySQL error 1792).
27 ///
28 /// # Examples
29 ///
30 /// ```
31 /// use toasty_core::Error;
32 ///
33 /// let err = Error::read_only_transaction("INSERT not allowed");
34 /// assert!(err.is_read_only_transaction());
35 /// assert_eq!(
36 /// err.to_string(),
37 /// "read-only transaction: INSERT not allowed"
38 /// );
39 /// ```
40 pub fn read_only_transaction(message: impl Into<String>) -> Error {
41 Error::from(super::ErrorKind::ReadOnlyTransaction(ReadOnlyTransaction {
42 message: message.into().into(),
43 }))
44 }
45
46 /// Returns `true` if this error is a read-only transaction error.
47 pub fn is_read_only_transaction(&self) -> bool {
48 matches!(self.kind(), super::ErrorKind::ReadOnlyTransaction(_))
49 }
50}