Skip to main content

potato_agent/agents/
error.rs

1use pyo3::exceptions::PyRuntimeError;
2use pyo3::PyErr;
3use thiserror::Error;
4use tracing::error;
5
6#[derive(Error, Debug)]
7pub enum AgentError {
8    #[error("Error: {0}")]
9    Error(String),
10
11    #[error("Failed to downcast Python object: {0}")]
12    DowncastError(String),
13
14    #[error("Failed to get environment variable: {0}")]
15    EnvVarError(#[from] std::env::VarError),
16
17    #[error("Failed to extract client: {0}")]
18    ClientExtractionError(String),
19
20    #[error("No ready tasks found but pending tasks remain. Possible circular dependency.")]
21    NoTaskFoundError,
22
23    #[error("Failed to create runtime: {0}")]
24    CreateRuntimeError(#[source] std::io::Error),
25
26    #[error(transparent)]
27    SerializationError(#[from] serde_json::Error),
28
29    #[error(transparent)]
30    UtilError(#[from] potato_util::UtilError),
31
32    #[error("Invalid output type: {0}")]
33    InvalidOutputType(String),
34
35    #[error("Failed to create tool: {0}")]
36    ToolCreationError(String),
37
38    #[error("Invalid tool definition")]
39    InvalidToolDefinitionError,
40
41    #[error("{0}")]
42    InvalidInput(String),
43
44    #[error("Provider mismatch: prompt provider {0}, agent provider {1}")]
45    ProviderMismatch(String, String),
46
47    #[error(transparent)]
48    ProviderError(#[from] potato_provider::error::ProviderError),
49
50    #[error("No provider specified for Agent")]
51    MissingProviderError,
52
53    #[error(transparent)]
54    TypeError(#[from] potato_type::TypeError),
55
56    #[error(transparent)]
57    StdIoError(#[from] std::io::Error),
58
59    #[error(transparent)]
60    StoreError(#[from] crate::agents::store::StoreError),
61
62    #[error("Not supported: {0}")]
63    NotSupportedError(String),
64
65    #[error("Output validation error: {0}")]
66    ValidationError(String),
67
68    #[error("Circular agent call detected: agent '{0}' is already in the call stack")]
69    CircularAgentCall(String),
70
71    #[error("Agent call disallowed by policy: agent '{0}'")]
72    DisallowedAgentCall(String),
73
74    #[error("Sub-agent calls are disallowed by policy")]
75    SubAgentCallsDisallowed,
76
77    #[error("Sub-agent needs user input: {0}")]
78    SubAgentNeedsInput(String),
79
80    #[error("Callback aborted run: {0}")]
81    CallbackAbort(String),
82
83    #[error("agent exceeded maximum iterations ({0})")]
84    MaxIterationsExceeded(u32),
85
86    #[error("internal lock was poisoned")]
87    LockPoisoned,
88}
89
90impl<'a, 'py> From<pyo3::CastError<'a, 'py>> for AgentError {
91    fn from(err: pyo3::CastError<'a, 'py>) -> Self {
92        AgentError::DowncastError(err.to_string())
93    }
94}
95
96impl From<AgentError> for PyErr {
97    fn from(err: AgentError) -> PyErr {
98        let msg = err.to_string();
99        error!("{}", msg);
100        PyRuntimeError::new_err(msg)
101    }
102}
103
104impl From<PyErr> for AgentError {
105    fn from(err: PyErr) -> Self {
106        AgentError::Error(err.to_string())
107    }
108}