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