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
use super::Error;
/// Error when a transaction is aborted due to a serialization conflict.
///
/// This maps to database-specific errors such as PostgreSQL SQLSTATE 40001
/// or MySQL error 1213. The transaction must be retried.
#[derive(Debug)]
pub(super) struct SerializationFailure {
message: Box<str>,
}
impl std::error::Error for SerializationFailure {}
impl core::fmt::Display for SerializationFailure {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "transaction serialization failure: {}", self.message)
}
}
impl Error {
/// Creates a serialization failure error.
///
/// Returned when the database aborts a transaction due to a serialization
/// conflict (e.g. PostgreSQL SQLSTATE 40001, MySQL error 1213).
///
/// # Examples
///
/// ```
/// use toasty_core::Error;
///
/// let err = Error::serialization_failure("concurrent update conflict");
/// assert!(err.is_serialization_failure());
/// ```
pub fn serialization_failure(message: impl Into<String>) -> Error {
Error::from(super::ErrorKind::SerializationFailure(
SerializationFailure {
message: message.into().into(),
},
))
}
/// Returns `true` if this error is a serialization failure.
pub fn is_serialization_failure(&self) -> bool {
matches!(self.kind(), super::ErrorKind::SerializationFailure(_))
}
}