1use std::fmt;
12
13mod build_error;
14
15pub use build_error::*;
16
17#[derive(Debug)]
23pub enum GraphError {
24 Terminal(TerminalError),
26}
27
28#[derive(Debug)]
30pub enum TerminalError {
31 InvalidGraph(String),
33 NodeNotFound(String),
35 MissingEdge { from: String, to: String },
37 NodeExecutionFailed {
39 node: String,
40 source: Box<dyn std::error::Error + Send + Sync>,
41 },
42 StepsExceeded { limit: usize },
44 LoopLimitExceeded { limit: usize },
46 BarrierTimeout {
48 node: String,
49 timeout: std::time::Duration,
50 },
51 BarrierCancelled { node: String },
53 Unrouted {
55 node: String,
57 attempted_conditions: Vec<ConditionEval>,
59 },
60 StateError(String),
62 CheckpointSaveFailed { error: String },
64 RestoreUnsupported { node: String, kind: String },
66 RestoreNotLatest { checkpoint: String, latest: String },
68 RestoreUnsupportedFormat { actual: u32, expected: u32 },
70 RestoreGraphMismatch { expected: u64, actual: u64 },
72 TraceNotFresh { trace: String, latest: String },
74 CheckpointLoadFailed { error: String },
76 RestoreFailed { reason: String },
78}
79
80#[derive(Debug, Clone)]
82pub enum ObservedError {
83 Warning { node: String, message: String },
85 Degraded { node: String, message: String },
87 PartialFailure {
89 node: String,
90 succeeded: usize,
91 failed: usize,
92 message: String,
93 },
94}
95
96impl fmt::Display for ObservedError {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 match self {
99 Self::Warning { node, message } => write!(f, "node '{}': {}", node, message),
100 Self::Degraded { node, message } => write!(f, "node '{}' degraded: {}", node, message),
101 Self::PartialFailure {
102 node,
103 succeeded,
104 failed,
105 message,
106 } => {
107 write!(
108 f,
109 "node '{}' partial: {}/{} ok, {}",
110 node,
111 succeeded,
112 succeeded + failed,
113 message
114 )
115 }
116 }
117 }
118}
119
120#[derive(Debug, Clone)]
122pub struct ConditionEval {
123 pub edge: String,
125 pub condition: Option<String>,
127 pub matched: bool,
129}
130
131impl fmt::Display for GraphError {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 Self::Terminal(e) => write!(f, "[terminal] {}", e),
137 }
138 }
139}
140
141impl fmt::Display for TerminalError {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 match self {
144 Self::InvalidGraph(msg) => write!(f, "invalid graph: {msg}"),
145 Self::NodeNotFound(name) => write!(f, "node not found: {name}"),
146 Self::MissingEdge { from, to } => {
147 write!(
148 f,
149 "goto '{}' from '{}' failed: no edge {}→{} exists",
150 to, from, from, to
151 )
152 }
153 Self::NodeExecutionFailed { node, source } => {
154 write!(f, "node '{node}' execution failed: {source}")
155 }
156 Self::StepsExceeded { limit } => {
157 write!(f, "step limit {limit} exceeded (potential infinite loop)")
158 }
159 Self::LoopLimitExceeded { limit } => write!(f, "loop limit exceeded: {limit}"),
160 Self::BarrierTimeout { node, timeout } => {
161 write!(f, "barrier '{node}' timed out after {timeout:?}")
162 }
163 Self::BarrierCancelled { node } => {
164 write!(
165 f,
166 "barrier '{node}' cancelled: consumer dropped the signal channel"
167 )
168 }
169 Self::Unrouted {
170 node,
171 attempted_conditions,
172 } => {
173 write!(f, "node '{}' has no matching outgoing edge", node)?;
174 if !attempted_conditions.is_empty() {
175 write!(f, ". evaluated: [")?;
176 for (i, ce) in attempted_conditions.iter().enumerate() {
177 if i > 0 {
178 write!(f, ", ")?;
179 }
180 write!(f, "{}={}", ce.edge, ce.matched)?;
181 }
182 write!(f, "]")?;
183 }
184 Ok(())
185 }
186 Self::StateError(msg) => write!(f, "state error: {msg}"),
187 Self::CheckpointSaveFailed { error } => {
188 write!(f, "checkpoint save failed: {error}")
189 }
190 Self::RestoreUnsupported { node, kind } => write!(
191 f,
192 "persistence/restore does not support {kind} node '{node}' (phase 1: serial + loops only)"
193 ),
194 Self::RestoreNotLatest { checkpoint, latest } => write!(
195 f,
196 "checkpoint {checkpoint} is not the latest of this trace (latest: {latest}); restore requires the latest checkpoint or a new trace"
197 ),
198 Self::RestoreUnsupportedFormat { actual, expected } => write!(
199 f,
200 "unsupported checkpoint format_version: {actual} (expected {expected})"
201 ),
202 Self::RestoreGraphMismatch { expected, actual } => write!(
203 f,
204 "graph hash mismatch: expected {expected:016x}, got {actual:016x}"
205 ),
206 Self::TraceNotFresh { trace, latest } => write!(
207 f,
208 "trace {trace} already has checkpoint {latest} (use restore entry)"
209 ),
210 Self::CheckpointLoadFailed { error } => {
211 write!(f, "checkpoint load failed: {error}")
212 }
213 Self::RestoreFailed { reason } => write!(f, "restore precondition failed: {reason}"),
214 }
215 }
216}
217
218impl std::error::Error for GraphError {
219 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
220 match self {
221 Self::Terminal(TerminalError::NodeExecutionFailed { source, .. }) => {
222 Some(source.as_ref())
223 }
224 Self::Terminal(_) => None,
225 }
226 }
227}
228
229impl std::error::Error for TerminalError {
230 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
231 match self {
232 Self::NodeExecutionFailed { source, .. } => Some(source.as_ref()),
233 _ => None,
234 }
235 }
236}