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("serialization error: {0}")]
42 Serialization(#[from] serde_json::Error),
43
44 #[error(
50 "{RUN_BUDGET_EXCEEDED_CODE}: run {run_id} would exceed its cost cap \
51 (spent {spent_usd} USD + next step {step_budget_usd} USD > cap {limit_usd} USD)"
52 )]
53 RunBudgetExceeded {
54 run_id: uuid::Uuid,
56 limit_usd: Decimal,
58 spent_usd: Decimal,
60 step_budget_usd: Decimal,
62 },
63
64 #[error(
68 "{MONTHLY_BUDGET_EXCEEDED_CODE}: monthly cost quota exhausted \
69 ({spent_usd} USD spent of {limit_usd} USD)"
70 )]
71 MonthlyBudgetExceeded {
72 limit_usd: Decimal,
74 spent_usd: Decimal,
76 },
77
78 #[error("step {step:?} declared output {pattern:?} but no file matched")]
84 MissingArtifact {
85 step: String,
87 pattern: String,
89 },
90
91 #[error("no artifact {name:?} produced by step {step:?} before this point")]
93 ArtifactNotFound {
94 step: String,
96 name: String,
98 },
99
100 #[error("artifact storage is not configured: {0}")]
102 ArtifactsUnavailable(String),
103
104 #[error("artifact storage error: {0}")]
106 Artifact(#[from] ArtifactError),
107
108 #[error("approval required for run {run_id}, step {step_id}: {message}")]
110 ApprovalRequired {
111 run_id: uuid::Uuid,
113 step_id: uuid::Uuid,
115 message: String,
117 },
118
119 #[error("{WORKFLOW_GUARD_REJECTED_CODE}: {0}")]
125 WorkflowGuardRejected(#[from] WorkflowRejection),
126}
127
128#[cfg(test)]
129mod tests {
130 use super::*;
131
132 #[test]
133 fn invalid_workflow_display() {
134 let err = EngineError::InvalidWorkflow("unknown-handler".to_string());
135 assert!(err.to_string().contains("invalid workflow"));
136 assert!(err.to_string().contains("unknown-handler"));
137 }
138
139 #[test]
140 fn step_config_display() {
141 let err = EngineError::StepConfig("bad shell config".to_string());
142 assert!(err.to_string().contains("step config error"));
143 assert!(err.to_string().contains("bad shell config"));
144 }
145
146 #[test]
147 fn store_error_from_conversion() {
148 let store_err = StoreError::RunNotFound(uuid::Uuid::nil());
149 let engine_err = EngineError::from(store_err);
150 assert!(engine_err.to_string().contains("store error"));
151 }
152
153 #[test]
154 fn run_budget_exceeded_display_carries_code_and_amounts() {
155 let err = EngineError::RunBudgetExceeded {
156 run_id: uuid::Uuid::nil(),
157 limit_usd: Decimal::new(200, 2),
158 spent_usd: Decimal::new(180, 2),
159 step_budget_usd: Decimal::new(50, 2),
160 };
161
162 let msg = err.to_string();
163 assert!(msg.contains(RUN_BUDGET_EXCEEDED_CODE));
164 assert!(msg.contains("2.00"));
165 assert!(msg.contains("1.80"));
166 assert!(msg.contains("0.50"));
167 }
168
169 #[test]
170 fn monthly_budget_exceeded_display_carries_code_and_amounts() {
171 let err = EngineError::MonthlyBudgetExceeded {
172 limit_usd: Decimal::new(10000, 2),
173 spent_usd: Decimal::new(10500, 2),
174 };
175
176 let msg = err.to_string();
177 assert!(msg.contains(MONTHLY_BUDGET_EXCEEDED_CODE));
178 assert!(msg.contains("100.00"));
179 assert!(msg.contains("105.00"));
180 }
181
182 #[test]
183 fn missing_artifact_display_names_the_step_and_pattern() {
184 let err = EngineError::MissingArtifact {
185 step: "build".to_string(),
186 pattern: "target/report.html".to_string(),
187 };
188
189 let msg = err.to_string();
190 assert!(msg.contains("\"build\""));
191 assert!(msg.contains("target/report.html"));
192 }
193
194 #[test]
195 fn artifact_not_found_display_names_the_producer() {
196 let err = EngineError::ArtifactNotFound {
197 step: "build".to_string(),
198 name: "report.html".to_string(),
199 };
200
201 let msg = err.to_string();
202 assert!(msg.contains("\"build\""));
203 assert!(msg.contains("report.html"));
204 }
205
206 #[test]
207 fn artifacts_unavailable_display() {
208 let err = EngineError::ArtifactsUnavailable("no blob store".to_string());
209 assert!(err.to_string().contains("not configured"));
210 }
211
212 #[test]
213 fn artifact_error_from_conversion() {
214 let engine_err = EngineError::from(ArtifactError::NotFound("a/b".to_string()));
215 assert!(engine_err.to_string().contains("artifact storage error"));
216 }
217
218 #[test]
219 fn serialization_error_from_conversion() {
220 let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
221 let engine_err = EngineError::from(serde_err);
222 assert!(engine_err.to_string().contains("serialization error"));
223 }
224
225 #[test]
226 fn workflow_guard_rejected_display_carries_code_and_detail() {
227 use crate::guard::WorkflowRejection;
228
229 let rejection = WorkflowRejection::MaxDepthExceeded { depth: 6, max: 5 };
230 let err = EngineError::from(rejection);
231
232 let msg = err.to_string();
233 assert!(msg.contains(WORKFLOW_GUARD_REJECTED_CODE));
234 assert!(msg.contains("max call depth exceeded"));
235 assert!(msg.contains("6/5"));
236 }
237
238 #[test]
239 fn workflow_guard_rejected_from_conversion() {
240 use crate::guard::WorkflowRejection;
241
242 let rejection = WorkflowRejection::CycleDetected {
243 target: "wf-b".to_string(),
244 chain: vec!["wf-a".to_string(), "wf-b".to_string()],
245 };
246 let engine_err = EngineError::from(rejection);
247 assert!(engine_err.to_string().contains("cycle detected"));
248 }
249}