Skip to main content

potato_provider/
error.rs

1use gcloud_auth::error::Error as GCloudAuthError;
2use pyo3::exceptions::PyRuntimeError;
3use pyo3::pyclass::PyClassGuardError;
4use pyo3::PyErr;
5use reqwest::StatusCode;
6use thiserror::Error;
7use tracing::error;
8
9#[derive(Error, Debug)]
10pub enum ProviderError {
11    #[error("Error: {0}")]
12    Error(String),
13
14    #[error("Failed to downcast Python object: {0}")]
15    DowncastError(String),
16
17    #[error("Client did not provide response")]
18    ClientNoResponseError,
19
20    #[error("Failed to create header value for the agent client")]
21    CreateHeaderValueError(#[from] reqwest::header::InvalidHeaderValue),
22
23    #[error("Failed to create header name for the agent client")]
24    CreateHeaderNameError(#[from] reqwest::header::InvalidHeaderName),
25
26    #[error("Failed to create agent client: {0}")]
27    CreateClientError(#[source] reqwest::Error),
28
29    #[error("Request failed: {0}")]
30    RequestError(#[from] reqwest::Error),
31
32    #[error("Failed to serialize chat request: {0}")]
33    SerializationError(#[from] serde_json::Error),
34
35    #[error("Failed to extract embedding config. Check provider and config compatibility: {0}")]
36    EmbeddingConfigExtractionError(String),
37
38    #[error("Missing authentication information. Failed to find API_KEY or credentials in environment variables.")]
39    MissingAuthenticationError,
40
41    #[error("Unsupported content type")]
42    UnsupportedContentTypeError,
43
44    #[error("Failed to get response: {0} with status code {1}")]
45    CompletionError(String, StatusCode),
46
47    #[error("Provider not supported: {0}")]
48    ProviderNotSupportedError(String),
49
50    #[error("No provider specified in GenAiClient")]
51    NoProviderError,
52
53    #[error("Undefined error: {0}")]
54    UndefinedError(String),
55
56    #[error("Invalid response type")]
57    InvalidResponseType(String),
58
59    #[error("Failed to create tokio runtime: {0}")]
60    RuntimeError(String),
61
62    #[error("No embeddings found in the response")]
63    NoEmbeddingsFound,
64
65    #[error(transparent)]
66    TypeError(#[from] potato_type::error::TypeError),
67
68    #[error(transparent)]
69    UtilError(#[from] potato_util::UtilError),
70
71    #[error(transparent)]
72    DecodeError(#[from] base64::DecodeError),
73
74    #[error(transparent)]
75    Utf8Error(#[from] std::string::FromUtf8Error),
76
77    #[error(transparent)]
78    GCloudAuthError(#[from] GCloudAuthError),
79
80    #[error("No Google credentials found in environment variables")]
81    NoCredentialsFound,
82
83    #[error("No project ID found in credentials or environment variables")]
84    NoProjectIdFound,
85
86    #[error("Failed to retrieve access token: {0}")]
87    TokenError(String),
88
89    #[error("Failed to retrieve OPENAI_API_KEY from the environment")]
90    MissingOpenAIApiKeyError,
91
92    #[error("{0}")]
93    NotImplementedError(String),
94
95    #[error("Method does not support PredictRequest")]
96    DoesNotSupportPredictRequest,
97
98    #[error("Method does not support array inputs")]
99    DoesNotSupportArray,
100
101    #[error("{0}")]
102    InvalidInputType(String),
103
104    #[error("{0}")]
105    InvalidConfigType(String),
106
107    #[error("Embedding not supported for this provider")]
108    EmbeddingNotSupported,
109
110    #[error(transparent)]
111    MacroError(#[from] potatohead_macro::error::MacroError),
112}
113
114impl<'a, 'py> From<pyo3::CastError<'a, 'py>> for ProviderError {
115    fn from(err: pyo3::CastError<'a, 'py>) -> Self {
116        ProviderError::DowncastError(err.to_string())
117    }
118}
119
120impl From<ProviderError> for PyErr {
121    fn from(err: ProviderError) -> PyErr {
122        let msg = err.to_string();
123        error!("{}", msg);
124        PyRuntimeError::new_err(msg)
125    }
126}
127
128impl From<PyErr> for ProviderError {
129    fn from(err: PyErr) -> Self {
130        ProviderError::Error(err.to_string())
131    }
132}
133
134impl<'a, 'py> From<PyClassGuardError<'a, 'py>> for ProviderError {
135    fn from(err: PyClassGuardError<'a, 'py>) -> Self {
136        ProviderError::Error(err.to_string())
137    }
138}