Skip to main content

adk_graph/
error.rs

1//! Error types for adk-graph
2
3use std::time::Duration;
4
5use crate::interrupt::Interrupt;
6use thiserror::Error;
7
8/// Result type for graph operations
9pub type Result<T> = std::result::Result<T, GraphError>;
10
11/// Errors that can occur during graph operations
12#[derive(Error, Debug)]
13pub enum GraphError {
14    /// Graph structure is invalid
15    #[error("Invalid graph structure: {0}")]
16    InvalidGraph(String),
17
18    /// Node not found
19    #[error("Node not found: {0}")]
20    NodeNotFound(String),
21
22    /// Edge target not found
23    #[error("Edge target not found: {0}")]
24    EdgeTargetNotFound(String),
25
26    /// No entry point defined
27    #[error("No entry point defined (missing edge from START)")]
28    NoEntryPoint,
29
30    /// Recursion limit exceeded
31    #[error("Recursion limit exceeded: {0} steps")]
32    RecursionLimitExceeded(usize),
33
34    /// Execution was interrupted
35    #[error("Execution interrupted: {0:?}")]
36    Interrupted(Box<InterruptedExecution>),
37
38    /// Node execution failed
39    #[error("Node '{node}' execution failed: {message}")]
40    NodeExecutionFailed { node: String, message: String },
41
42    /// Node timed out
43    #[error("Node '{node}' timed out after {elapsed:?}")]
44    NodeTimedOut { node: String, elapsed: Duration },
45
46    /// Fan-in node timed out waiting for upstream paths
47    #[error("Fan-in node '{node}' timed out: received {received}/{expected} upstream outputs")]
48    FanInTimedOut { node: String, received: usize, expected: usize },
49
50    /// State serialization error
51    #[error("State serialization error: {0}")]
52    SerializationError(String),
53
54    /// Checkpoint error
55    #[error("Checkpoint error: {0}")]
56    CheckpointError(String),
57
58    /// Router returned unknown target
59    #[error("Router returned unknown target: {0}")]
60    UnknownRouteTarget(String),
61
62    /// IO error
63    #[error("IO error: {0}")]
64    IoError(#[from] std::io::Error),
65
66    /// JSON error
67    #[error("JSON error: {0}")]
68    JsonError(#[from] serde_json::Error),
69
70    /// Database error (when sqlite feature enabled)
71    #[cfg(feature = "sqlite")]
72    #[error("Database error: {0}")]
73    DatabaseError(#[from] sqlx::Error),
74
75    /// Other error (used by extensions like the functional API)
76    #[error("{0}")]
77    Other(String),
78}
79
80/// Information about an interrupted execution
81#[derive(Debug, Clone)]
82pub struct InterruptedExecution {
83    /// Thread ID for resumption
84    pub thread_id: String,
85    /// Checkpoint ID for resumption
86    pub checkpoint_id: String,
87    /// The interrupt that occurred
88    pub interrupt: Interrupt,
89    /// Current state at interruption
90    pub state: crate::state::State,
91    /// Step number when interrupted
92    pub step: usize,
93}
94
95impl InterruptedExecution {
96    /// Create a new interrupted execution
97    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}