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("no artifact {name:?} produced by step {step:?} before this point")]
108 ArtifactNotFound {
109 step: String,
111 name: String,
113 },
114
115 #[error("artifact storage is not configured: {0}")]
117 ArtifactsUnavailable(String),
118
119 #[error("artifact storage error: {0}")]
121 Artifact(#[from] ArtifactError),
122
123 #[error("approval required for run {run_id}, step {step_id}: {message}")]
125 ApprovalRequired {
126 run_id: uuid::Uuid,
128 step_id: uuid::Uuid,
130 message: String,
132 },
133
134 #[error("delay sleeping for run {run_id}, step {step_id}: wake at {wake_at}")]
140 DelaySleeping {
141 run_id: uuid::Uuid,
143 step_id: uuid::Uuid,
145 wake_at: chrono::DateTime<chrono::Utc>,
147 },
148
149 #[error("{WORKFLOW_GUARD_REJECTED_CODE}: {0}")]
155 WorkflowGuardRejected(#[from] WorkflowRejection),
156}
157
158#[cfg(test)]
159mod tests {
160 use super::*;
161
162 #[test]
163 fn invalid_workflow_display() {
164 let err = EngineError::InvalidWorkflow("unknown-handler".to_string());
165 assert!(err.to_string().contains("invalid workflow"));
166 assert!(err.to_string().contains("unknown-handler"));
167 }
168
169 #[test]
170 fn step_config_display() {
171 let err = EngineError::StepConfig("bad shell config".to_string());
172 assert!(err.to_string().contains("step config error"));
173 assert!(err.to_string().contains("bad shell config"));
174 }
175
176 #[test]
177 fn store_error_from_conversion() {
178 let store_err = StoreError::RunNotFound(uuid::Uuid::nil());
179 let engine_err = EngineError::from(store_err);
180 assert!(engine_err.to_string().contains("store error"));
181 }
182
183 #[test]
184 fn run_budget_exceeded_display_carries_code_and_amounts() {
185 let err = EngineError::RunBudgetExceeded {
186 run_id: uuid::Uuid::nil(),
187 limit_usd: Decimal::new(200, 2),
188 spent_usd: Decimal::new(180, 2),
189 step_budget_usd: Decimal::new(50, 2),
190 };
191
192 let msg = err.to_string();
193 assert!(msg.contains(RUN_BUDGET_EXCEEDED_CODE));
194 assert!(msg.contains("2.00"));
195 assert!(msg.contains("1.80"));
196 assert!(msg.contains("0.50"));
197 }
198
199 #[test]
200 fn monthly_budget_exceeded_display_carries_code_and_amounts() {
201 let err = EngineError::MonthlyBudgetExceeded {
202 limit_usd: Decimal::new(10000, 2),
203 spent_usd: Decimal::new(10500, 2),
204 };
205
206 let msg = err.to_string();
207 assert!(msg.contains(MONTHLY_BUDGET_EXCEEDED_CODE));
208 assert!(msg.contains("100.00"));
209 assert!(msg.contains("105.00"));
210 }
211
212 #[test]
213 fn missing_artifact_display_names_the_step_and_pattern() {
214 let err = EngineError::MissingArtifact {
215 step: "build".to_string(),
216 pattern: "target/report.html".to_string(),
217 };
218
219 let msg = err.to_string();
220 assert!(msg.contains("\"build\""));
221 assert!(msg.contains("target/report.html"));
222 }
223
224 #[test]
225 fn artifact_not_found_display_names_the_producer() {
226 let err = EngineError::ArtifactNotFound {
227 step: "build".to_string(),
228 name: "report.html".to_string(),
229 };
230
231 let msg = err.to_string();
232 assert!(msg.contains("\"build\""));
233 assert!(msg.contains("report.html"));
234 }
235
236 #[test]
237 fn artifacts_unavailable_display() {
238 let err = EngineError::ArtifactsUnavailable("no blob store".to_string());
239 assert!(err.to_string().contains("not configured"));
240 }
241
242 #[test]
243 fn artifact_error_from_conversion() {
244 let engine_err = EngineError::from(ArtifactError::NotFound("a/b".to_string()));
245 assert!(engine_err.to_string().contains("artifact storage error"));
246 }
247
248 #[test]
249 fn serialization_error_from_conversion() {
250 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
251 let engine_err = EngineError::from(serde_err);
252 assert!(engine_err.to_string().contains("serialization error"));
253 }
254
255 #[test]
256 fn workflow_guard_rejected_display_carries_code_and_detail() {
257 use crate::guard::WorkflowRejection;
258
259 let rejection = WorkflowRejection::MaxDepthExceeded { depth: 6, max: 5 };
260 let err = EngineError::from(rejection);
261
262 let msg = err.to_string();
263 assert!(msg.contains(WORKFLOW_GUARD_REJECTED_CODE));
264 assert!(msg.contains("max call depth exceeded"));
265 assert!(msg.contains("6/5"));
266 }
267
268 #[test]
269 fn workflow_guard_rejected_from_conversion() {
270 use crate::guard::WorkflowRejection;
271
272 let rejection = WorkflowRejection::CycleDetected {
273 target: "wf-b".to_string(),
274 chain: vec!["wf-a".to_string(), "wf-b".to_string()],
275 };
276 let engine_err = EngineError::from(rejection);
277 assert!(engine_err.to_string().contains("cycle detected"));
278 }
279}