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
49
50
51
52
53
54
55
use super::Error;
/// Error when a conditional operation's condition evaluates to false.
///
/// This occurs when:
/// - An UPDATE with a WHERE clause matches no rows (condition didn't match)
/// - A DynamoDB conditional write fails (ConditionalCheckFailedException)
/// - An optimistic lock version check fails
#[derive(Debug)]
pub(super) struct ConditionFailed {
context: Option<Box<str>>,
}
impl std::error::Error for ConditionFailed {}
impl core::fmt::Display for ConditionFailed {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("condition failed")?;
if let Some(ref ctx) = self.context {
write!(f, ": {}", ctx)?;
}
Ok(())
}
}
impl Error {
/// Creates a condition failed error.
///
/// This is used when a conditional operation's condition evaluates to false, such as:
/// - An UPDATE with a WHERE clause that matches no rows
/// - A DynamoDB conditional write that fails
/// - An optimistic lock version check that fails
///
/// The context parameter provides information about what condition failed.
///
/// # Examples
///
/// ```
/// use toasty_core::Error;
///
/// let err = Error::condition_failed("optimistic lock version mismatch");
/// assert!(err.is_condition_failed());
/// assert_eq!(err.to_string(), "condition failed: optimistic lock version mismatch");
/// ```
pub fn condition_failed(context: impl Into<String>) -> Error {
Error::from(super::ErrorKind::ConditionFailed(ConditionFailed {
context: Some(context.into().into()),
}))
}
/// Returns `true` if this error is a condition failed error.
pub fn is_condition_failed(&self) -> bool {
matches!(self.kind(), super::ErrorKind::ConditionFailed(_))
}
}