From ad56357f880b8f537d7c818822d750775ef57b79 Mon Sep 17 00:00:00 2001 From: erhant Date: Fri, 8 Nov 2024 14:14:35 +0300 Subject: [PATCH 1/2] added non-2XX status code handlers --- src/api_interface/gem_api.rs | 37 +++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/api_interface/gem_api.rs b/src/api_interface/gem_api.rs index 9c92ae7..60badc6 100644 --- a/src/api_interface/gem_api.rs +++ b/src/api_interface/gem_api.rs @@ -5,7 +5,7 @@ use ollama_rs::{ }; use reqwest::Client; use serde_json::{json, Value}; -use std::sync::Arc; +use std::{error::Error, sync::Arc}; pub struct GeminiExecutor { model: String, @@ -80,7 +80,18 @@ impl GeminiExecutor { .json(&body) .send() .await - .map_err(|e| OllamaError::from(format!("Gemini API request failed: {}", e)))?; + .map_err(|e| { + OllamaError::from(format!("Gemini API request failed: {:?}", e.source())) + })?; + + // check status + if let Err(e) = response.error_for_status_ref() { + return Err(OllamaError::from(format!( + "Gemini API request failed with status {}: {:?}", + response.status(), + e.source() + ))); + } let response_body: Value = response.json().await.map_err(|e| { OllamaError::from(format!("Failed to parse Gemini API response: {}", e)) @@ -167,7 +178,27 @@ impl GeminiExecutor { .json(&body) .send() .await - .map_err(|e| OllamaError::from(format!("Gemini API request failed: {:?}", e)))?; + .map_err(|e| { + OllamaError::from(format!("Gemini API request failed: {:?}", e.source())) + })?; + + // check status + if let Err(e) = response.error_for_status_ref() { + return Err(OllamaError::from(format!( + "Gemini API request failed with status {}: {:?}", + response.status(), + e.source() + ))); + } + + // check status + if let Err(e) = response.error_for_status_ref() { + return Err(OllamaError::from(format!( + "Gemini API request failed with status {}: {:?}", + response.status(), + e.source() + ))); + } let response_body: Value = response.json().await.map_err(|e| { OllamaError::from(format!("Failed to parse Gemini API response: {:?}", e)) From a8eb059dd81de8d214d0ab75e003c58feb3090ff Mon Sep 17 00:00:00 2001 From: erhant Date: Fri, 8 Nov 2024 14:16:05 +0300 Subject: [PATCH 2/2] rm extra code --- src/api_interface/gem_api.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/api_interface/gem_api.rs b/src/api_interface/gem_api.rs index 60badc6..9a2f4ce 100644 --- a/src/api_interface/gem_api.rs +++ b/src/api_interface/gem_api.rs @@ -191,15 +191,6 @@ impl GeminiExecutor { ))); } - // check status - if let Err(e) = response.error_for_status_ref() { - return Err(OllamaError::from(format!( - "Gemini API request failed with status {}: {:?}", - response.status(), - e.source() - ))); - } - let response_body: Value = response.json().await.map_err(|e| { OllamaError::from(format!("Failed to parse Gemini API response: {:?}", e)) })?;