feat: check response status in all GeminiClient methods and add Error::is_retryable() (#10)
Some checks failed
Rust / build (pull_request) Failing after 3s

This commit is contained in:
2026-10-26 19:24:49 +00:00
parent 7264276232
commit 8d1c9c3bb6
3 changed files with 306 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use super::*;
use crate::types::PredictImageRequestParameters;
type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
@@ -203,3 +204,203 @@ async fn stream_generate_content_yields_error_on_truncated_sse() -> Result<()> {
}
Ok(())
}
#[tokio::test]
async fn generate_content_returns_http_status_on_non_json_503() -> Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = socket.read(&mut buf).await;
let body = "Service Unavailable";
let response = format!(
"HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let client = GeminiClient::new(
MockTokenProvider,
format!("http://{addr}"),
"test-project".to_string(),
"test-location".to_string(),
);
let request = GenerateContentRequest::default();
let result = client.generate_content(&request, "gemini-test").await;
match result {
Err(Error::HttpStatus { status, body }) => {
assert_eq!(status, reqwest::StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body, "Service Unavailable");
}
Err(other) => panic!("Expected Error::HttpStatus, got: {other:?}"),
Ok(_) => panic!("Expected error response, got Ok"),
}
Ok(())
}
#[tokio::test]
async fn generate_content_returns_vertex_error_on_400_json() -> Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = socket.read(&mut buf).await;
let body = r#"{"error": {"code": 400, "message": "Invalid request", "status": "INVALID_ARGUMENT"}}"#;
let response = format!(
"HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let client = GeminiClient::new(
MockTokenProvider,
format!("http://{addr}"),
"test-project".to_string(),
"test-location".to_string(),
);
let request = GenerateContentRequest::default();
let result = client.generate_content(&request, "gemini-test").await;
match result {
Err(Error::VertexError(err)) => {
assert_eq!(err.code, 400);
assert_eq!(err.status, "INVALID_ARGUMENT");
}
Err(other) => panic!("Expected Error::VertexError, got: {other:?}"),
Ok(_) => panic!("Expected error response, got Ok"),
}
Ok(())
}
#[tokio::test]
async fn count_tokens_returns_http_status_on_non_json_error() -> Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = socket.read(&mut buf).await;
let body = "Bad Gateway";
let response = format!(
"HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let client = GeminiClient::new(
MockTokenProvider,
format!("http://{addr}"),
"test-project".to_string(),
"test-location".to_string(),
);
let request = CountTokensRequest {
contents: Content::default(),
};
let result = client.count_tokens(&request, "gemini-test").await;
match result {
Err(Error::HttpStatus { status, body }) => {
assert_eq!(status, reqwest::StatusCode::BAD_GATEWAY);
assert_eq!(body, "Bad Gateway");
}
Err(other) => panic!("Expected Error::HttpStatus, got: {other:?}"),
Ok(_) => panic!("Expected error response, got Ok"),
}
Ok(())
}
#[tokio::test]
async fn text_embeddings_returns_http_status_on_non_json_error() -> Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = socket.read(&mut buf).await;
let body = "Internal Server Error";
let response = format!(
"HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let client = GeminiClient::new(
MockTokenProvider,
format!("http://{addr}"),
"test-project".to_string(),
"test-location".to_string(),
);
let request = TextEmbeddingRequest { instances: vec![] };
let result = client.text_embeddings(&request, "gemini-test").await;
match result {
Err(Error::HttpStatus { status, body }) => {
assert_eq!(status, reqwest::StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(body, "Internal Server Error");
}
Err(other) => panic!("Expected Error::HttpStatus, got: {other:?}"),
Ok(_) => panic!("Expected error response, got Ok"),
}
Ok(())
}
#[tokio::test]
async fn predict_image_returns_http_status_on_non_json_error() -> Result<()> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let addr = listener.local_addr()?;
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut buf = [0u8; 4096];
let _ = socket.read(&mut buf).await;
let body = "Service Unavailable";
let response = format!(
"HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
socket.write_all(response.as_bytes()).await.unwrap();
});
let client = GeminiClient::new(
MockTokenProvider,
format!("http://{addr}"),
"test-project".to_string(),
"test-location".to_string(),
);
let request = PredictImageRequest {
instances: vec![],
parameters: PredictImageRequestParameters::default(),
};
let result = client.predict_image(&request, "imagen-test").await;
match result {
Err(Error::HttpStatus { status, body }) => {
assert_eq!(status, reqwest::StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(body, "Service Unavailable");
}
Err(other) => panic!("Expected Error::HttpStatus, got: {other:?}"),
Ok(_) => panic!("Expected error response, got Ok"),
}
Ok(())
}