gem_rs/errors.rs
1//! Error types for the Gem-rs library.
2//!
3//! This module defines custom error types used throughout the Gem-rs library,
4//! providing detailed information about various error conditions that may occur
5//! during interactions with the Gemini API.
6
7use std::error::Error;
8use std::fmt;
9
10use crate::types;
11
12/// Represents errors that can occur in the Gem-rs library.
13#[derive(Debug)]
14pub enum GemError {
15 /// Indicates that an empty response was received from the API.
16 EmptyApiResponse,
17
18 /// Represents a connection error when communicating with the API.
19 ConnectionError(reqwest::Error),
20
21 /// Represents an error in the API response, including the HTTP status code.
22 ResponseError((reqwest::Error, reqwest::StatusCode)),
23
24 /// Indicates that the prompt feedback was blocked by the API.
25 PromptFeedbackBlocked,
26
27 /// Indicates that all candidate responses were blocked by the API.
28 AllCandidatesBlocked,
29
30 /// Represents an error returned by the Gemini API.
31 GeminiAPIError(types::Error),
32
33 /// Represents an error that occurred while parsing the API response.
34 ParsingError(serde_json::Error),
35
36 /// Represents an error that occurred during the feedback process.
37 FeedbackError(String),
38
39 /// Represents an error that occurred while streaming data.
40 StreamError(String),
41
42 /// Represents an error related to file operations.
43 FileError(String),
44}
45
46impl fmt::Display for GemError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 GemError::EmptyApiResponse => write!(f, "Received an empty response from the API"),
50 GemError::PromptFeedbackBlocked => write!(f, "Prompt feedback state is blocked"),
51 GemError::AllCandidatesBlocked => write!(f, "All candidates have a block error"),
52 GemError::ConnectionError(e) => write!(f, "Connection error: {}", e),
53 GemError::ParsingError(e) => write!(f, "Parsing error: {}", e),
54 GemError::GeminiAPIError(e) => write!(f, "Gemini API error: {}", e),
55 GemError::ResponseError((e, status)) => {
56 write!(f, "Response error: {} (status code: {})", e, status)
57 }
58 GemError::FeedbackError(e) => write!(f, "Feedback error: {}", e),
59 GemError::StreamError(e) => write!(f, "Stream error: {}", e),
60 GemError::FileError(e) => write!(f, "File error: {}", e),
61 }
62 }
63}
64
65impl Error for GemError {}
66
67/// Represents the reason why the Gemini API finished generating content.
68#[derive(Debug)]
69pub enum FinishReason {
70 /// Indicates that the generation was stopped due to safety concerns.
71 Safety,
72
73 /// Indicates that the generation was stopped due to content recitation.
74 Recitation,
75
76 /// Represents any other reason for finishing content generation.
77 Other,
78}
79
80impl FinishReason {
81 /// Determines if the finish reason is considered a block error.
82 ///
83 /// # Returns
84 ///
85 /// `true` if the finish reason is either `Safety` or `Recitation`, `false` otherwise.
86 pub fn is_block_error(&self) -> bool {
87 matches!(self, FinishReason::Safety | FinishReason::Recitation)
88 }
89}