1use rust_decimal::Decimal;
4use thiserror::Error;
5
6use ironflow_artifacts::error::ArtifactError;
7use ironflow_core::error::OperationError;
8use ironflow_store::error::StoreError;
9
10use crate::guard::{WORKFLOW_GUARD_REJECTED_CODE, WorkflowRejection};
11
12pub const RUN_BUDGET_EXCEEDED_CODE: &str = "RUN_BUDGET_EXCEEDED";
14
15pub const MONTHLY_BUDGET_EXCEEDED_CODE: &str = "MONTHLY_BUDGET_EXCEEDED";
17
18pub const HANDLER_VERSION_MISMATCH_CODE: &str = "HANDLER_VERSION_MISMATCH";
20
21#[derive(Debug, Error)]
23pub enum EngineError {
24 #[error("operation failed: {0}")]
26 Operation(#[from] OperationError),
27
28 #[error("store error: {0}")]
30 Store(#[from] StoreError),
31
32 #[error("invalid workflow: {0}")]
34 InvalidWorkflow(String),
35
36 #[error("step config error: {0}")]
38 StepConfig(String),
39
40 #[error("decision error: {0}")]
42 Decision(#[from] ironflow_core::error::DecisionError),
43
44 #[error(
47 "decision step '{step}' requires a decision provider; \
48 wire one with Engine::with_decision_provider(...)"
49 )]
50 NoDecisionProvider {
51 step: String,
53 },
54
55 #[error("serialization error: {0}")]
57 Serialization(#[from] serde_json::Error),
58
59 #[error(
65 "{RUN_BUDGET_EXCEEDED_CODE}: run {run_id} would exceed its cost cap \
66 (spent {spent_usd} USD + next step {step_budget_usd} USD > cap {limit_usd} USD)"
67 )]
68 RunBudgetExceeded {
69 run_id: uuid::Uuid,
71 limit_usd: Decimal,
73 spent_usd: Decimal,
75 step_budget_usd: Decimal,
77 },
78
79 #[error(
83 "{MONTHLY_BUDGET_EXCEEDED_CODE}: monthly cost quota exhausted \
84 ({spent_usd} USD spent of {limit_usd} USD)"
85 )]
86 MonthlyBudgetExceeded {
87 limit_usd: Decimal,
89 spent_usd: Decimal,
91 },
92
93 #[error("step {step:?} declared output {pattern:?} but no file matched")]
99 MissingArtifact {
100 step: String,
102 pattern: String,
104 },
105
106 #[error("step {step:?} declares no artifact output named {name:?}")]
108 ArtifactNotDeclared {
109 step: String,
111 name: String,
113 },
114
115 #[error("no artifact {name:?} produced by step {step:?} before this point")]
117 ArtifactNotFound {
118 step: String,
120 name: String,
122 },
123
124 #[error("artifact storage is not configured: {0}")]
126 ArtifactsUnavailable(String),
127
128 #[error("artifact storage error: {0}")]
130 Artifact(#[from] ArtifactError),
131
132 #[error("approval required for run {run_id}, step {step_id}: {message}")]
134 ApprovalRequired {
135 run_id: uuid::Uuid,
137 step_id: uuid::Uuid,
139 message: String,
141 },
142
143 #[error("approval rejected for run {run_id}, step {step_id}: {reason}")]
150 ApprovalRejected {
151 run_id: uuid::Uuid,
153 step_id: uuid::Uuid,
155 reason: String,
157 },
158
159 #[error("delay sleeping for run {run_id}, step {step_id}: wake at {wake_at}")]
165 DelaySleeping {
166 run_id: uuid::Uuid,
168 step_id: uuid::Uuid,
170 wake_at: chrono::DateTime<chrono::Utc>,
172 },
173
174 #[error("{WORKFLOW_GUARD_REJECTED_CODE}: {0}")]
180 WorkflowGuardRejected(#[from] WorkflowRejection),
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186
187 #[test]
188 fn invalid_workflow_display() {
189 let err = EngineError::InvalidWorkflow("unknown-handler".to_string());
190 assert!(err.to_string().contains("invalid workflow"));
191 assert!(err.to_string().contains("unknown-handler"));
192 }
193
194 #[test]
195 fn step_config_display() {
196 let err = EngineError::StepConfig("bad shell config".to_string());
197 assert!(err.to_string().contains("step config error"));
198 assert!(err.to_string().contains("bad shell config"));
199 }
200
201 #[test]
202 fn store_error_from_conversion() {
203 let store_err = StoreError::RunNotFound(uuid::Uuid::nil());
204 let engine_err = EngineError::from(store_err);
205 assert!(engine_err.to_string().contains("store error"));
206 }
207
208 #[test]
209 fn run_budget_exceeded_display_carries_code_and_amounts() {
210 let err = EngineError::RunBudgetExceeded {
211 run_id: uuid::Uuid::nil(),
212 limit_usd: Decimal::new(200, 2),
213 spent_usd: Decimal::new(180, 2),
214 step_budget_usd: Decimal::new(50, 2),
215 };
216
217 let msg = err.to_string();
218 assert!(msg.contains(RUN_BUDGET_EXCEEDED_CODE));
219 assert!(msg.contains("2.00"));
220 assert!(msg.contains("1.80"));
221 assert!(msg.contains("0.50"));
222 }
223
224 #[test]
225 fn monthly_budget_exceeded_display_carries_code_and_amounts() {
226 let err = EngineError::MonthlyBudgetExceeded {
227 limit_usd: Decimal::new(10000, 2),
228 spent_usd: Decimal::new(10500, 2),
229 };
230
231 let msg = err.to_string();
232 assert!(msg.contains(MONTHLY_BUDGET_EXCEEDED_CODE));
233 assert!(msg.contains("100.00"));
234 assert!(msg.contains("105.00"));
235 }
236
237 #[test]
238 fn missing_artifact_display_names_the_step_and_pattern() {
239 let err = EngineError::MissingArtifact {
240 step: "build".to_string(),
241 pattern: "target/report.html".to_string(),
242 };
243
244 let msg = err.to_string();
245 assert!(msg.contains("\"build\""));
246 assert!(msg.contains("target/report.html"));
247 }
248
249 #[test]
250 fn artifact_not_declared_display_names_the_step_and_artifact() {
251 let err = EngineError::ArtifactNotDeclared {
252 step: "build".to_string(),
253 name: "report.htm".to_string(),
254 };
255
256 let msg = err.to_string();
257 assert!(msg.contains("\"build\""));
258 assert!(msg.contains("\"report.htm\""));
259 }
260
261 #[test]
262 fn artifact_not_found_display_names_the_producer() {
263 let err = EngineError::ArtifactNotFound {
264 step: "build".to_string(),
265 name: "report.html".to_string(),
266 };
267
268 let msg = err.to_string();
269 assert!(msg.contains("\"build\""));
270 assert!(msg.contains("report.html"));
271 }
272
273 #[test]
274 fn artifacts_unavailable_display() {
275 let err = EngineError::ArtifactsUnavailable("no blob store".to_string());
276 assert!(err.to_string().contains("not configured"));
277 }
278
279 #[test]
280 fn artifact_error_from_conversion() {
281 let engine_err = EngineError::from(ArtifactError::NotFound("a/b".to_string()));
282 assert!(engine_err.to_string().contains("artifact storage error"));
283 }
284
285 #[test]
286 fn serialization_error_from_conversion() {
287 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
288 let engine_err = EngineError::from(serde_err);
289 assert!(engine_err.to_string().contains("serialization error"));
290 }
291
292 #[test]
293 fn workflow_guard_rejected_display_carries_code_and_detail() {
294 use crate::guard::WorkflowRejection;
295
296 let rejection = WorkflowRejection::MaxDepthExceeded { depth: 6, max: 5 };
297 let err = EngineError::from(rejection);
298
299 let msg = err.to_string();
300 assert!(msg.contains(WORKFLOW_GUARD_REJECTED_CODE));
301 assert!(msg.contains("max call depth exceeded"));
302 assert!(msg.contains("6/5"));
303 }
304
305 #[test]
306 fn workflow_guard_rejected_from_conversion() {
307 use crate::guard::WorkflowRejection;
308
309 let rejection = WorkflowRejection::CycleDetected {
310 target: "wf-b".to_string(),
311 chain: vec!["wf-a".to_string(), "wf-b".to_string()],
312 };
313 let engine_err = EngineError::from(rejection);
314 assert!(engine_err.to_string().contains("cycle detected"));
315 }
316}