potato_agent/agents/
error.rs

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