1use std::time::Duration;
4
5use crate::interrupt::Interrupt;
6use thiserror::Error;
7
8pub type Result<T> = std::result::Result<T, GraphError>;
10
11#[derive(Error, Debug)]
13pub enum GraphError {
14 #[error("Invalid graph structure: {0}")]
16 InvalidGraph(String),
17
18 #[error("Node not found: {0}")]
20 NodeNotFound(String),
21
22 #[error("Edge target not found: {0}")]
24 EdgeTargetNotFound(String),
25
26 #[error("No entry point defined (missing edge from START)")]
28 NoEntryPoint,
29
30 #[error("Recursion limit exceeded: {0} steps")]
32 RecursionLimitExceeded(usize),
33
34 #[error("Execution interrupted: {0:?}")]
36 Interrupted(Box<InterruptedExecution>),
37
38 #[error("Node '{node}' execution failed: {message}")]
40 NodeExecutionFailed { node: String, message: String },
41
42 #[error("Node '{node}' timed out after {elapsed:?}")]
44 NodeTimedOut { node: String, elapsed: Duration },
45
46 #[error("Fan-in node '{node}' timed out: received {received}/{expected} upstream outputs")]
48 FanInTimedOut { node: String, received: usize, expected: usize },
49
50 #[error("State serialization error: {0}")]
52 SerializationError(String),
53
54 #[error("Checkpoint error: {0}")]
56 CheckpointError(String),
57
58 #[error("Router returned unknown target: {0}")]
60 UnknownRouteTarget(String),
61
62 #[error("IO error: {0}")]
64 IoError(#[from] std::io::Error),
65
66 #[error("JSON error: {0}")]
68 JsonError(#[from] serde_json::Error),
69
70 #[cfg(feature = "sqlite")]
72 #[error("Database error: {0}")]
73 DatabaseError(#[from] sqlx::Error),
74
75 #[error("{0}")]
77 Other(String),
78}
79
80#[derive(Debug, Clone)]
82pub struct InterruptedExecution {
83 pub thread_id: String,
85 pub checkpoint_id: String,
87 pub interrupt: Interrupt,
89 pub state: crate::state::State,
91 pub step: usize,
93}
94
95impl InterruptedExecution {
96 pub fn new(
98 thread_id: String,
99 checkpoint_id: String,
100 interrupt: Interrupt,
101 state: crate::state::State,
102 step: usize,
103 ) -> Self {
104 Self { thread_id, checkpoint_id, interrupt, state, step }
105 }
106}
107
108impl From<GraphError> for adk_core::AdkError {
109 fn from(err: GraphError) -> Self {
110 use adk_core::{ErrorCategory, ErrorComponent};
111 let (category, code) = match &err {
112 GraphError::InvalidGraph(_) => (ErrorCategory::InvalidInput, "graph.invalid"),
113 GraphError::NodeNotFound(_) => (ErrorCategory::NotFound, "graph.node_not_found"),
114 GraphError::EdgeTargetNotFound(_) => {
115 (ErrorCategory::NotFound, "graph.edge_target_not_found")
116 }
117 GraphError::NoEntryPoint => (ErrorCategory::InvalidInput, "graph.no_entry_point"),
118 GraphError::RecursionLimitExceeded(_) => {
119 (ErrorCategory::Internal, "graph.recursion_limit")
120 }
121 GraphError::Interrupted(_) => (ErrorCategory::Cancelled, "graph.interrupted"),
122 GraphError::NodeExecutionFailed { .. } => {
123 (ErrorCategory::Internal, "graph.node_execution_failed")
124 }
125 GraphError::NodeTimedOut { .. } => (ErrorCategory::Timeout, "graph.node_timed_out"),
126 GraphError::FanInTimedOut { .. } => (ErrorCategory::Timeout, "graph.fan_in_timed_out"),
127 GraphError::SerializationError(_) => (ErrorCategory::Internal, "graph.serialization"),
128 GraphError::CheckpointError(_) => (ErrorCategory::Internal, "graph.checkpoint"),
129 GraphError::UnknownRouteTarget(_) => {
130 (ErrorCategory::NotFound, "graph.unknown_route_target")
131 }
132 GraphError::IoError(_) => (ErrorCategory::Internal, "graph.io"),
133 GraphError::JsonError(_) => (ErrorCategory::Internal, "graph.json"),
134 #[cfg(feature = "sqlite")]
135 GraphError::DatabaseError(_) => (ErrorCategory::Internal, "graph.database"),
136 GraphError::Other(_) => (ErrorCategory::Internal, "graph.other"),
137 };
138 adk_core::AdkError::new(ErrorComponent::Graph, category, code, err.to_string())
139 .with_source(err)
140 }
141}