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
76#[derive(Debug, Clone)]
78pub struct InterruptedExecution {
79 pub thread_id: String,
81 pub checkpoint_id: String,
83 pub interrupt: Interrupt,
85 pub state: crate::state::State,
87 pub step: usize,
89}
90
91impl InterruptedExecution {
92 pub fn new(
94 thread_id: String,
95 checkpoint_id: String,
96 interrupt: Interrupt,
97 state: crate::state::State,
98 step: usize,
99 ) -> Self {
100 Self { thread_id, checkpoint_id, interrupt, state, step }
101 }
102}
103
104impl From<GraphError> for adk_core::AdkError {
105 fn from(err: GraphError) -> Self {
106 use adk_core::{ErrorCategory, ErrorComponent};
107 let (category, code) = match &err {
108 GraphError::InvalidGraph(_) => (ErrorCategory::InvalidInput, "graph.invalid"),
109 GraphError::NodeNotFound(_) => (ErrorCategory::NotFound, "graph.node_not_found"),
110 GraphError::EdgeTargetNotFound(_) => {
111 (ErrorCategory::NotFound, "graph.edge_target_not_found")
112 }
113 GraphError::NoEntryPoint => (ErrorCategory::InvalidInput, "graph.no_entry_point"),
114 GraphError::RecursionLimitExceeded(_) => {
115 (ErrorCategory::Internal, "graph.recursion_limit")
116 }
117 GraphError::Interrupted(_) => (ErrorCategory::Cancelled, "graph.interrupted"),
118 GraphError::NodeExecutionFailed { .. } => {
119 (ErrorCategory::Internal, "graph.node_execution_failed")
120 }
121 GraphError::NodeTimedOut { .. } => (ErrorCategory::Timeout, "graph.node_timed_out"),
122 GraphError::FanInTimedOut { .. } => (ErrorCategory::Timeout, "graph.fan_in_timed_out"),
123 GraphError::SerializationError(_) => (ErrorCategory::Internal, "graph.serialization"),
124 GraphError::CheckpointError(_) => (ErrorCategory::Internal, "graph.checkpoint"),
125 GraphError::UnknownRouteTarget(_) => {
126 (ErrorCategory::NotFound, "graph.unknown_route_target")
127 }
128 GraphError::IoError(_) => (ErrorCategory::Internal, "graph.io"),
129 GraphError::JsonError(_) => (ErrorCategory::Internal, "graph.json"),
130 #[cfg(feature = "sqlite")]
131 GraphError::DatabaseError(_) => (ErrorCategory::Internal, "graph.database"),
132 };
133 adk_core::AdkError::new(ErrorComponent::Graph, category, code, err.to_string())
134 .with_source(err)
135 }
136}