1use thiserror::Error;
7use uuid::Uuid;
8
9use crate::entities::{ConcurrencyLimitError, 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("invalid concurrency limit: {0}")]
85 InvalidConcurrencyLimit(#[from] ConcurrencyLimitError),
86
87 #[error("schedule not found: {0}")]
89 ScheduleNotFound(Uuid),
90
91 #[error("approval delegation not found: {0}")]
93 DelegationNotFound(Uuid),
94
95 #[error("provider account not found: {0}")]
97 ProviderAccountNotFound(Uuid),
98
99 #[error("provider account already exists: {0}")]
101 DuplicateProviderAccount(String),
102
103 #[error("database error: {0}")]
105 Database(String),
106
107 #[error("serialization error: {0}")]
109 Serialization(#[from] serde_json::Error),
110
111 #[error("crypto error: {0}")]
113 Crypto(String),
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn run_not_found_display() {
122 let id = Uuid::nil();
123 let err = StoreError::RunNotFound(id);
124 assert_eq!(err.to_string(), format!("run not found: {id}"));
125 }
126
127 #[test]
128 fn step_not_found_display() {
129 let id = Uuid::nil();
130 let err = StoreError::StepNotFound(id);
131 assert_eq!(err.to_string(), format!("step not found: {id}"));
132 }
133
134 #[test]
135 fn invalid_transition_display() {
136 let err = StoreError::InvalidTransition {
137 from: RunStatus::Pending,
138 to: RunStatus::Completed,
139 };
140 assert!(err.to_string().contains("Pending"));
141 assert!(err.to_string().contains("Completed"));
142 }
143
144 #[test]
145 fn concurrency_conflict_display() {
146 let run_id = Uuid::now_v7();
147 let err = StoreError::ConcurrencyConflict {
148 key: "issue:12".to_string(),
149 run_id,
150 };
151 let message = err.to_string();
152 assert!(message.contains("issue:12"));
153 assert!(message.contains(&run_id.to_string()));
154 }
155
156 #[test]
157 fn invalid_concurrency_limit_display() {
158 let err = StoreError::from(ConcurrencyLimitError::EmptyGroup);
159 assert_eq!(
160 err.to_string(),
161 "invalid concurrency limit: concurrency group must not be empty"
162 );
163 }
164
165 #[test]
166 fn delegation_not_found_display() {
167 let id = Uuid::nil();
168 let err = StoreError::DelegationNotFound(id);
169 assert_eq!(
170 err.to_string(),
171 format!("approval delegation not found: {id}")
172 );
173 }
174
175 #[test]
176 fn database_error_display() {
177 let err = StoreError::Database("connection refused".to_string());
178 assert!(err.to_string().contains("connection refused"));
179 }
180
181 #[test]
182 fn serialization_error_from_serde() {
183 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
184 let err = StoreError::from(serde_err);
185 assert!(err.to_string().contains("serialization error"));
186 }
187}