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("Not supported: {0}")]
60    NotSupportedError(String),
61
62    #[error("Output validation error: {0}")]
63    ValidationError(String),
64}
65
66impl<'a, 'py> From<pyo3::CastError<'a, 'py>> for AgentError {
67    fn from(err: pyo3::CastError<'a, 'py>) -> Self {
68        AgentError::DowncastError(err.to_string())
69    }
70}
71
72impl From<AgentError> for PyErr {
73    fn from(err: AgentError) -> PyErr {
74        let msg = err.to_string();
75        error!("{}", msg);
76        PyRuntimeError::new_err(msg)
77    }
78}
79
80impl From<PyErr> for AgentError {
81    fn from(err: PyErr) -> Self {
82        AgentError::Error(err.to_string())
83    }
84}