Skip to main content

gemini_client_api/gemini/
error.rs

1use reqwest::StatusCode;
2use serde::{Deserialize, Deserializer};
3use serde_json::Value;
4
5#[derive(Deserialize, thiserror::Error, Debug, PartialEq, Eq)]
6#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
7pub enum Status {
8    #[error("The request body is malformed.")]
9    ///The request body is malformed.
10    InvalidArgument,
11    #[error(
12        "Gemini API free tier is not available in your country. Please enable billing on your project in Google AI Studio."
13    )]
14    ///Gemini API free tier is not available in your country. Please enable billing on your project in Google AI Studio.
15    FailedPrecondition,
16    #[error("Your API key doesn't have the required permissions.")]
17    ///Your API key doesn't have the required permissions.
18    PermissionDenied,
19    #[error("The requested resource wasn't found.")]
20    ///The requested resource wasn't found.
21    NotFound,
22    #[error("You've exceeded the rate limit.")]
23    ///You've exceeded the rate limit.
24    ResourceExhausted,
25    #[error("An unexpected error occurred on Google's side.")]
26    ///An unexpected error occurred on Google's side.
27    Internal,
28    #[error("The service may be temporarily overloaded or down.")]
29    ///The service may be temporarily overloaded or down.
30    Unavailable,
31    #[error("The service is unable to finish processing within the deadline.")]
32    ///The service is unable to finish processing within the deadline.
33    DeadlineExceeded,
34}
35fn deserialize_status_code<'de, D>(deserializer: D) -> Result<StatusCode, D::Error>
36where
37    D: Deserializer<'de>,
38{
39    let s = u16::deserialize(deserializer)?;
40    StatusCode::from_u16(s).map_err(serde::de::Error::custom)
41}
42#[derive(Deserialize, thiserror::Error, Debug)]
43#[error("{code}, {status}\nMessage: {message}\nDetails: {:#?}", details.as_ref().unwrap_or(&vec![]))]
44pub struct Error {
45    #[serde(deserialize_with = "deserialize_status_code")]
46    pub code: StatusCode,
47    pub message: String,
48    pub status: Status,
49    pub details: Option<Vec<Value>>,
50}
51
52#[derive(Deserialize, thiserror::Error, Debug)]
53#[error("Gemini API Error: {error}")]
54pub struct GeminiError {
55    pub error: Error,
56}
57#[derive(thiserror::Error, Debug)]
58pub enum GeminiResponseError {
59    #[error(transparent)]
60    #[cfg(feature = "reqwest")]
61    ReqwestError(reqwest::Error),
62    #[error("Response status not Ok.\n{0}")]
63    StatusNotOk(GeminiError),
64    #[error("Cannot Respond if last Chat has Role::Model")]
65    ///Cannot Responnd if last Chat has Role::Model
66    NothingToRespond,
67}
68
69#[derive(thiserror::Error, Debug)]
70pub enum GeminiResponseStreamError {
71    #[error(transparent)]
72    #[cfg(feature = "reqwest")]
73    ReqwestError(reqwest::Error),
74    #[error("Invalid Response Format received. Response: {0}")]
75    ///Invalid Response Format received. Contains response string
76    InvalidResposeFormat(String),
77}