Skip to main content

adk_graph/functional/
error.rs

1//! Error types for the functional API.
2
3use crate::error::GraphError;
4
5/// Errors specific to the functional API.
6#[derive(Debug, thiserror::Error)]
7pub enum FunctionalError {
8    /// Task execution failed after all retry attempts.
9    #[error("task '{task}' failed after {attempts} attempts: {message}")]
10    TaskFailed {
11        /// Name of the failed task.
12        task: String,
13        /// Number of attempts made.
14        attempts: u32,
15        /// Failure message.
16        message: String,
17    },
18
19    /// State schema validation error.
20    #[error("state validation failed for field '{field}': expected {expected}, got {actual}")]
21    SchemaValidation {
22        /// The field that failed validation.
23        field: String,
24        /// The expected type or value.
25        expected: String,
26        /// The actual type or value.
27        actual: String,
28    },
29
30    /// The workflow suspended at an interrupt and needs a resume value.
31    ///
32    /// `continuation_key` identifies the interrupt site. Supply the value under that key via
33    /// `ExecutionConfig::with_resume_value` (or `TaskContext::with_resume_values`) and re-invoke
34    /// the entrypoint; the interrupt call then returns the value instead of this error.
35    ///
36    /// Previously an interrupt always produced `InterruptTypeMismatch`, so a caller could not
37    /// tell "needs input" from "the value you gave me was the wrong type" — and there was no
38    /// key to supply the value under.
39    #[error("workflow suspended at interrupt '{continuation_key}': {message}")]
40    Suspended {
41        /// Key identifying this interrupt site within the thread.
42        continuation_key: String,
43        /// The message passed to `interrupt`.
44        message: String,
45    },
46
47    /// Interrupt deserialization error (wrong type provided on resume).
48    #[error("interrupt resume type mismatch for task '{task}': {message}")]
49    InterruptTypeMismatch {
50        /// The task that was interrupted.
51        task: String,
52        /// Description of the type mismatch.
53        message: String,
54    },
55
56    /// Workflow was cancelled via cancellation token.
57    #[error("workflow cancelled")]
58    Cancelled,
59
60    /// Checkpoint persistence failure.
61    #[error("checkpoint failed for task '{task}': {message}")]
62    CheckpointFailed {
63        /// The task whose checkpoint failed.
64        task: String,
65        /// Description of the checkpoint failure.
66        message: String,
67    },
68
69    /// Background run timeout exceeded.
70    #[error("run '{run_id}' timed out after {timeout_secs}s")]
71    RunTimeout {
72        /// The run identifier.
73        run_id: String,
74        /// The timeout duration in seconds.
75        timeout_secs: u64,
76    },
77
78    /// Invalid cron expression.
79    #[error("invalid cron expression '{expression}': {reason}")]
80    InvalidCronExpression {
81        /// The invalid cron expression.
82        expression: String,
83        /// Reason the expression is invalid.
84        reason: String,
85    },
86
87    /// Cron job not found.
88    #[error("cron job '{job_id}' not found")]
89    CronJobNotFound {
90        /// The job identifier that was not found.
91        job_id: String,
92    },
93
94    /// Background run not found.
95    #[error("run '{run_id}' not found")]
96    RunNotFound {
97        /// The run identifier that was not found.
98        run_id: String,
99    },
100}
101
102/// A suspension and a cancellation keep their meaning as
103/// [`GraphError::Suspended`] and [`GraphError::Cancelled`], so a `#[task]` retry
104/// policy can tell them from failures; every other error becomes
105/// [`GraphError::Other`].
106impl From<FunctionalError> for GraphError {
107    fn from(e: FunctionalError) -> Self {
108        match e {
109            FunctionalError::Suspended { continuation_key, message } => {
110                GraphError::Suspended { continuation_key, message }
111            }
112            FunctionalError::Cancelled => GraphError::Cancelled,
113            other => GraphError::Other(other.to_string()),
114        }
115    }
116}