potato_agent/agents/
error.rs

1use potato_prompt::PromptError;
2use pyo3::exceptions::PyRuntimeError;
3use pyo3::PyErr;
4use reqwest::StatusCode;
5use thiserror::Error;
6use tracing::error;
7
8#[derive(Error, Debug)]
9pub enum AgentError {
10    #[error("Error: {0}")]
11    Error(String),
12
13    #[error("Failed to create header value for the agent client")]
14    CreateHeaderValueError(#[from] reqwest::header::InvalidHeaderValue),
15
16    #[error("Failed to create header name for the agent client")]
17    CreateHeaderNameError(#[from] reqwest::header::InvalidHeaderName),
18
19    #[error("Failed to create agent client: {0}")]
20    CreateClientError(#[source] reqwest::Error),
21
22    #[error("Request failed: {0}")]
23    RequestError(#[from] reqwest::Error),
24
25    #[error("Failed to serialize chat request: {0}")]
26    SerializationError(#[from] serde_json::Error),
27
28    #[error("Failed to get chat completion response: {0} with status code {1}")]
29    ChatCompletionError(String, StatusCode),
30
31    #[error("Failed to downcast Python object: {0}")]
32    DowncastError(String),
33
34    #[error("Failed to get environment variable: {0}")]
35    EnvVarError(#[from] std::env::VarError),
36
37    #[error("Failed to retrieve GEMINI_API_KEY from the environment")]
38    MissingGeminiApiKeyError,
39
40    #[error("Failed to retrieve OPENAI_API_KEY from the environment")]
41    MissingOpenAIApiKeyError,
42
43    #[error("Failed to extract client: {0}")]
44    ClientExtractionError(String),
45
46    #[error("Client did not provide response")]
47    ClientNoResponseError,
48
49    #[error("No ready tasks found but pending tasks remain. Possible circular dependency.")]
50    NoTaskFoundError,
51
52    #[error("Unsupported content type")]
53    UnsupportedContentTypeError,
54
55    #[error("Failed to create runtime: {0}")]
56    CreateRuntimeError(#[source] std::io::Error),
57
58    #[error(transparent)]
59    PromptError(#[from] PromptError),
60
61    #[error(transparent)]
62    UtilError(#[from] potato_util::UtilError),
63
64    #[error(transparent)]
65    TypeError(#[from] potato_type::error::TypeError),
66
67    #[error("Invalid output type: {0}")]
68    InvalidOutputType(String),
69
70    #[error("Failed to create tokio runtime: {0}")]
71    RuntimeError(String),
72
73    #[error("Undefined error: {0}")]
74    UndefinedError(String),
75}
76
77impl<'a> From<pyo3::DowncastError<'a, 'a>> for AgentError {
78    fn from(err: pyo3::DowncastError) -> Self {
79        AgentError::DowncastError(err.to_string())
80    }
81}
82
83impl From<AgentError> for PyErr {
84    fn from(err: AgentError) -> PyErr {
85        let msg = err.to_string();
86        error!("{}", msg);
87        PyRuntimeError::new_err(msg)
88    }
89}
90
91impl From<PyErr> for AgentError {
92    fn from(err: PyErr) -> Self {
93        AgentError::Error(err.to_string())
94    }
95}