Skip to main content

ironflow_store/
error.rs

1//! Storage error types.
2//!
3//! [`StoreError`] covers all failure modes for [`RunStore`](crate::store::RunStore)
4//! implementations, from missing records to invalid FSM transitions.
5
6use thiserror::Error;
7use uuid::Uuid;
8
9use crate::entities::RunStatus;
10
11/// Errors produced by [`RunStore`](crate::store::RunStore) operations.
12///
13/// # Examples
14///
15/// ```
16/// use ironflow_store::error::StoreError;
17/// use uuid::Uuid;
18///
19/// let err = StoreError::RunNotFound(Uuid::nil());
20/// assert!(err.to_string().contains("run not found"));
21/// ```
22#[derive(Debug, Error)]
23pub enum StoreError {
24    /// The requested run does not exist.
25    #[error("run not found: {0}")]
26    RunNotFound(Uuid),
27
28    /// The requested step does not exist.
29    #[error("step not found: {0}")]
30    StepNotFound(Uuid),
31
32    /// Attempted an illegal FSM transition.
33    #[error("invalid status transition: {from} -> {to}")]
34    InvalidTransition {
35        /// Current status.
36        from: RunStatus,
37        /// Attempted target status.
38        to: RunStatus,
39    },
40
41    /// Email is already taken by another user.
42    #[error("email already exists: {0}")]
43    DuplicateEmail(String),
44
45    /// Username is already taken by another user.
46    #[error("username already exists: {0}")]
47    DuplicateUsername(String),
48
49    /// The requested user does not exist.
50    #[error("user not found: {0}")]
51    UserNotFound(Uuid),
52
53    /// The worker no longer holds the lease on this run.
54    ///
55    /// Either another worker took it over after the lease expired, or the run
56    /// left the `Running` state (cancelled, failed, awaiting approval).
57    #[error("lease lost on run {run_id}")]
58    LeaseLost {
59        /// Run whose lease was lost.
60        run_id: Uuid,
61        /// Worker currently holding the lease, if any.
62        held_by: Option<String>,
63    },
64
65    /// The step already holds an artifact with this name.
66    #[error("artifact {name:?} already exists on step {step_id}")]
67    DuplicateArtifact {
68        /// Step that already owns the name.
69        step_id: Uuid,
70        /// The conflicting artifact name.
71        name: String,
72    },
73
74    /// The requested schedule does not exist.
75    #[error("schedule not found: {0}")]
76    ScheduleNotFound(Uuid),
77
78    /// The requested approval delegation does not exist.
79    #[error("approval delegation not found: {0}")]
80    DelegationNotFound(Uuid),
81
82    /// A database or I/O error from the backing store.
83    #[error("database error: {0}")]
84    Database(String),
85
86    /// JSON serialization or deserialization failed.
87    #[error("serialization error: {0}")]
88    Serialization(#[from] serde_json::Error),
89
90    /// A cryptographic operation failed (encryption or decryption).
91    #[error("crypto error: {0}")]
92    Crypto(String),
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn run_not_found_display() {
101        let id = Uuid::nil();
102        let err = StoreError::RunNotFound(id);
103        assert_eq!(err.to_string(), format!("run not found: {id}"));
104    }
105
106    #[test]
107    fn step_not_found_display() {
108        let id = Uuid::nil();
109        let err = StoreError::StepNotFound(id);
110        assert_eq!(err.to_string(), format!("step not found: {id}"));
111    }
112
113    #[test]
114    fn invalid_transition_display() {
115        let err = StoreError::InvalidTransition {
116            from: RunStatus::Pending,
117            to: RunStatus::Completed,
118        };
119        assert!(err.to_string().contains("Pending"));
120        assert!(err.to_string().contains("Completed"));
121    }
122
123    #[test]
124    fn delegation_not_found_display() {
125        let id = Uuid::nil();
126        let err = StoreError::DelegationNotFound(id);
127        assert_eq!(
128            err.to_string(),
129            format!("approval delegation not found: {id}")
130        );
131    }
132
133    #[test]
134    fn database_error_display() {
135        let err = StoreError::Database("connection refused".to_string());
136        assert!(err.to_string().contains("connection refused"));
137    }
138
139    #[test]
140    fn serialization_error_from_serde() {
141        let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
142        let err = StoreError::from(serde_err);
143        assert!(err.to_string().contains("serialization error"));
144    }
145}