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
76/// Information about an interrupted execution
77#[derive(Debug, Clone)]
78pub struct InterruptedExecution {
79    /// Thread ID for resumption
80    pub thread_id: String,
81    /// Checkpoint ID for resumption
82    pub checkpoint_id: String,
83    /// The interrupt that occurred
84    pub interrupt: Interrupt,
85    /// Current state at interruption
86    pub state: crate::state::State,
87    /// Step number when interrupted
88    pub step: usize,
89}
90
91impl InterruptedExecution {
92    /// Create a new interrupted execution
93    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}