1use thiserror::Error;
7use uuid::Uuid;
8
9use crate::entities::RunStatus;
10
11#[derive(Debug, Error)]
23pub enum StoreError {
24 #[error("run not found: {0}")]
26 RunNotFound(Uuid),
27
28 #[error("step not found: {0}")]
30 StepNotFound(Uuid),
31
32 #[error("invalid status transition: {from} -> {to}")]
34 InvalidTransition {
35 from: RunStatus,
37 to: RunStatus,
39 },
40
41 #[error("email already exists: {0}")]
43 DuplicateEmail(String),
44
45 #[error("username already exists: {0}")]
47 DuplicateUsername(String),
48
49 #[error("user not found: {0}")]
51 UserNotFound(Uuid),
52
53 #[error("lease lost on run {run_id}")]
58 LeaseLost {
59 run_id: Uuid,
61 held_by: Option<String>,
63 },
64
65 #[error("artifact {name:?} already exists on step {step_id}")]
67 DuplicateArtifact {
68 step_id: Uuid,
70 name: String,
72 },
73
74 #[error("concurrency key {key:?} is held by active run {run_id}")]
76 ConcurrencyConflict {
77 key: String,
79 run_id: Uuid,
81 },
82
83 #[error("schedule not found: {0}")]
85 ScheduleNotFound(Uuid),
86
87 #[error("approval delegation not found: {0}")]
89 DelegationNotFound(Uuid),
90
91 #[error("provider account not found: {0}")]
93 ProviderAccountNotFound(Uuid),
94
95 #[error("provider account already exists: {0}")]
97 DuplicateProviderAccount(String),
98
99 #[error("database error: {0}")]
101 Database(String),
102
103 #[error("serialization error: {0}")]
105 Serialization(#[from] serde_json::Error),
106
107 #[error("crypto error: {0}")]
109 Crypto(String),
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn run_not_found_display() {
118 let id = Uuid::nil();
119 let err = StoreError::RunNotFound(id);
120 assert_eq!(err.to_string(), format!("run not found: {id}"));
121 }
122
123 #[test]
124 fn step_not_found_display() {
125 let id = Uuid::nil();
126 let err = StoreError::StepNotFound(id);
127 assert_eq!(err.to_string(), format!("step not found: {id}"));
128 }
129
130 #[test]
131 fn invalid_transition_display() {
132 let err = StoreError::InvalidTransition {
133 from: RunStatus::Pending,
134 to: RunStatus::Completed,
135 };
136 assert!(err.to_string().contains("Pending"));
137 assert!(err.to_string().contains("Completed"));
138 }
139
140 #[test]
141 fn concurrency_conflict_display() {
142 let run_id = Uuid::now_v7();
143 let err = StoreError::ConcurrencyConflict {
144 key: "issue:12".to_string(),
145 run_id,
146 };
147 let message = err.to_string();
148 assert!(message.contains("issue:12"));
149 assert!(message.contains(&run_id.to_string()));
150 }
151
152 #[test]
153 fn delegation_not_found_display() {
154 let id = Uuid::nil();
155 let err = StoreError::DelegationNotFound(id);
156 assert_eq!(
157 err.to_string(),
158 format!("approval delegation not found: {id}")
159 );
160 }
161
162 #[test]
163 fn database_error_display() {
164 let err = StoreError::Database("connection refused".to_string());
165 assert!(err.to_string().contains("connection refused"));
166 }
167
168 #[test]
169 fn serialization_error_from_serde() {
170 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
171 let err = StoreError::from(serde_err);
172 assert!(err.to_string().contains("serialization error"));
173 }
174}