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
use super::Error;
/// Error when a write operation is attempted inside a read-only transaction.
///
/// This maps to database-specific errors such as PostgreSQL SQLSTATE 25006
/// or MySQL error 1792.
#[derive(Debug)]
pub(super) struct ReadOnlyTransaction {
message: Box<str>,
}
impl std::error::Error for ReadOnlyTransaction {}
impl core::fmt::Display for ReadOnlyTransaction {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "read-only transaction: {}", self.message)
}
}
impl Error {
/// Creates a read-only transaction error.
///
/// Returned when a write operation is attempted inside a read-only
/// transaction (e.g. PostgreSQL SQLSTATE 25006, MySQL error 1792).
///
/// # Examples
///
/// ```
/// use toasty_core::Error;
///
/// let err = Error::read_only_transaction("INSERT not allowed");
/// assert!(err.is_read_only_transaction());
/// assert_eq!(
/// err.to_string(),
/// "read-only transaction: INSERT not allowed"
/// );
/// ```
pub fn read_only_transaction(message: impl Into<String>) -> Error {
Error::from(super::ErrorKind::ReadOnlyTransaction(ReadOnlyTransaction {
message: message.into().into(),
}))
}
/// Returns `true` if this error is a read-only transaction error.
pub fn is_read_only_transaction(&self) -> bool {
matches!(self.kind(), super::ErrorKind::ReadOnlyTransaction(_))
}
}