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/// This maps to database-specific errors such as PostgreSQL SQLSTATE 25006
6/// or MySQL error 1792.
7#[derive(Debug)]
8pub(super) struct ReadOnlyTransaction {
9 message: Box<str>,
10}
11
12impl std::error::Error for ReadOnlyTransaction {}
13
14impl core::fmt::Display for ReadOnlyTransaction {
15 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
16 write!(f, "read-only transaction: {}", self.message)
17 }
18}
19
20impl Error {
21 /// Creates a read-only transaction error.
22 ///
23 /// Returned when a write operation is attempted inside a read-only
24 /// transaction (e.g. PostgreSQL SQLSTATE 25006, MySQL error 1792).
25 ///
26 /// # Examples
27 ///
28 /// ```
29 /// use toasty_core::Error;
30 ///
31 /// let err = Error::read_only_transaction("INSERT not allowed");
32 /// assert!(err.is_read_only_transaction());
33 /// assert_eq!(
34 /// err.to_string(),
35 /// "read-only transaction: INSERT not allowed"
36 /// );
37 /// ```
38 pub fn read_only_transaction(message: impl Into<String>) -> Error {
39 Error::from(super::ErrorKind::ReadOnlyTransaction(ReadOnlyTransaction {
40 message: message.into().into(),
41 }))
42 }
43
44 /// Returns `true` if this error is a read-only transaction error.
45 pub fn is_read_only_transaction(&self) -> bool {
46 matches!(self.kind(), super::ErrorKind::ReadOnlyTransaction(_))
47 }
48}