gemini_rust/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur when using the Gemini API
4#[derive(Error, Debug)]
5pub enum Error {
6    /// Error from the reqwest HTTP client
7    #[error("HTTP error: {0}")]
8    HttpError(#[from] reqwest::Error),
9
10    /// Error parsing JSON
11    #[error("JSON error: {0}")]
12    JsonError(#[from] serde_json::Error),
13
14    /// Error from the Gemini API
15    #[error("Gemini API error: {status_code} - {message}")]
16    ApiError {
17        /// HTTP status code
18        status_code: u16,
19        /// Error message
20        message: String,
21    },
22
23    /// Error building a valid request
24    #[error("Request building error: {0}")]
25    RequestError(String),
26
27    /// Missing API key
28    #[error("Missing API key")]
29    MissingApiKey,
30
31    /// Error with function calls
32    #[error("Function call error: {0}")]
33    FunctionCallError(String),
34
35    /// Error converting between types
36    #[error("Try from error: {0}")]
37    TryFromError(String),
38
39    /// Error indicating a batch operation has failed
40    #[error("Batch operation failed: {name}")]
41    BatchFailed { name: String, error: OperationError },
42
43    /// Error indicating a batch operation has expired
44    #[error("Batch operation expired: {name}")]
45    BatchExpired { name: String },
46
47    /// Error indicating an inconsistent state from the Batch API
48    #[error("Inconsistent batch state: {description}")]
49    InconsistentBatchState { description: String },
50
51    /// Error indicating the batch operation resulted in an unsupported output format (e.g., GCS)
52    #[error("Unsupported batch output: {description}")]
53    UnsupportedBatchOutput { description: String },
54}
55
56/// Represents an error within a long-running operation.
57#[derive(Debug, serde::Deserialize, serde::Serialize)]
58pub struct OperationError {
59    pub code: i32,
60    pub message: String,
61    // details are not included as they are not consistently typed in the API
62}
63
64impl From<serde_json::Value> for Error {
65    fn from(value: serde_json::Value) -> Self {
66        Error::TryFromError(value.to_string())
67    }
68}