toasty_core/error/
condition_failed.rs

1use super::Error;
2
3/// Error when a conditional operation's condition evaluates to false.
4///
5/// This occurs when:
6/// - An UPDATE with a WHERE clause matches no rows (condition didn't match)
7/// - A DynamoDB conditional write fails (ConditionalCheckFailedException)
8/// - An optimistic lock version check fails
9#[derive(Debug)]
10pub(super) struct ConditionFailed {
11    context: Option<Box<str>>,
12}
13
14impl std::error::Error for ConditionFailed {}
15
16impl core::fmt::Display for ConditionFailed {
17    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
18        f.write_str("condition failed")?;
19        if let Some(ref ctx) = self.context {
20            write!(f, ": {}", ctx)?;
21        }
22        Ok(())
23    }
24}
25
26impl Error {
27    /// Creates a condition failed error.
28    ///
29    /// This is used when a conditional operation's condition evaluates to false, such as:
30    /// - An UPDATE with a WHERE clause that matches no rows
31    /// - A DynamoDB conditional write that fails
32    /// - An optimistic lock version check that fails
33    ///
34    /// The context parameter provides information about what condition failed.
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// use toasty_core::Error;
40    ///
41    /// let err = Error::condition_failed("optimistic lock version mismatch");
42    /// assert!(err.is_condition_failed());
43    /// assert_eq!(err.to_string(), "condition failed: optimistic lock version mismatch");
44    /// ```
45    pub fn condition_failed(context: impl Into<String>) -> Error {
46        Error::from(super::ErrorKind::ConditionFailed(ConditionFailed {
47            context: Some(context.into().into()),
48        }))
49    }
50
51    /// Returns `true` if this error is a condition failed error.
52    pub fn is_condition_failed(&self) -> bool {
53        matches!(self.kind(), super::ErrorKind::ConditionFailed(_))
54    }
55}