Compare commits
5 Commits
f7cc9977aa
...
feat/retry
| Author | SHA1 | Date | |
|---|---|---|---|
| a49d37f03d | |||
| cecc14d31e | |||
| 4791125943 | |||
| 6d49f9939c | |||
| c911fc382e |
18
.github/workflows/rust.yml
vendored
18
.github/workflows/rust.yml
vendored
@@ -10,13 +10,19 @@ env:
|
|||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
check:
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Build
|
# The Gitea runner image doesn't ship with Rust, unlike GitHub's.
|
||||||
run: cargo build --verbose
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- name: Run tests
|
with:
|
||||||
run: cargo test --verbose
|
components: clippy, rustfmt
|
||||||
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
- name: Format
|
||||||
|
run: cargo fmt --check
|
||||||
|
- name: Clippy
|
||||||
|
run: cargo clippy --all-targets -- -D warnings
|
||||||
|
- name: Test
|
||||||
|
run: cargo test
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "gemini-rs"
|
name = "gemini-rs"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use console::style;
|
use console::style;
|
||||||
use dialoguer::{theme::ColorfulTheme, Input};
|
use dialoguer::{Input, theme::ColorfulTheme};
|
||||||
use gemini_rs::prelude::*;
|
use gemini_rs::prelude::*;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.add_text_part("Answer as if you were Yoda")
|
.add_text_part("Answer as if you were Yoda")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let user_prompt = vec![Content::builder()
|
let user_prompt = vec![
|
||||||
.role(Role::User)
|
Content::builder()
|
||||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
.role(Role::User)
|
||||||
.build()];
|
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||||
|
.build(),
|
||||||
|
];
|
||||||
|
|
||||||
let request = GenerateContentRequest::builder()
|
let request = GenerateContentRequest::builder()
|
||||||
.contents(user_prompt)
|
.contents(user_prompt)
|
||||||
|
|||||||
@@ -1,34 +1,36 @@
|
|||||||
use gemini_rs::prelude::*;
|
use gemini_rs::prelude::*;
|
||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let authentication_manager = gcp_auth::provider().await?;
|
let authentication_manager = gcp_auth::provider().await?;
|
||||||
let api_endpoint = std::env::var("API_ENDPOINT")?;
|
let api_endpoint = std::env::var("API_ENDPOINT")?;
|
||||||
let project_id = std::env::var("PROJECT_ID")?;
|
let project_id = std::env::var("PROJECT_ID")?;
|
||||||
let location_id = std::env::var("LOCATION_ID")?;
|
let location_id = std::env::var("LOCATION_ID")?;
|
||||||
|
|
||||||
let gemini = GeminiClient::new(
|
let gemini = GeminiClient::new(
|
||||||
authentication_manager,
|
authentication_manager,
|
||||||
api_endpoint,
|
api_endpoint,
|
||||||
project_id,
|
project_id,
|
||||||
location_id,
|
location_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let prompt = vec![Content::builder()
|
let prompt = vec![
|
||||||
.role(Role::User)
|
Content::builder()
|
||||||
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
|
.role(Role::User)
|
||||||
.build()];
|
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
|
||||||
|
.build(),
|
||||||
let request = GenerateContentRequest::builder().contents(prompt).build();
|
];
|
||||||
|
|
||||||
let mut queue = gemini
|
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||||
.stream_generate_content(&request, "gemini-2.0-flash-001")
|
|
||||||
.await?;
|
let mut queue = gemini
|
||||||
|
.stream_generate_content(&request, "gemini-2.0-flash-001")
|
||||||
while let Some(response) = queue.next().await {
|
.await?;
|
||||||
println!("Response: {:?}", response?);
|
|
||||||
}
|
while let Some(response) = queue.next().await {
|
||||||
|
println!("Response: {:?}", response?);
|
||||||
Ok(())
|
}
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,27 +1,29 @@
|
|||||||
use gemini_rs::prelude::*;
|
use gemini_rs::prelude::*;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let authentication_manager = gcp_auth::provider().await?;
|
let authentication_manager = gcp_auth::provider().await?;
|
||||||
let api_endpoint = std::env::var("API_ENDPOINT")?;
|
let api_endpoint = std::env::var("API_ENDPOINT")?;
|
||||||
let project_id = std::env::var("PROJECT_ID")?;
|
let project_id = std::env::var("PROJECT_ID")?;
|
||||||
let location_id = std::env::var("LOCATION_ID")?;
|
let location_id = std::env::var("LOCATION_ID")?;
|
||||||
|
|
||||||
let gemini = GeminiClient::new(
|
let gemini = GeminiClient::new(
|
||||||
authentication_manager,
|
authentication_manager,
|
||||||
api_endpoint,
|
api_endpoint,
|
||||||
project_id,
|
project_id,
|
||||||
location_id,
|
location_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let prompt = vec![Content::builder()
|
let prompt = vec![
|
||||||
.role(Role::User)
|
Content::builder()
|
||||||
.add_text_part("What is the airspeed of an unladen swallow?")
|
.role(Role::User)
|
||||||
.build()];
|
.add_text_part("What is the airspeed of an unladen swallow?")
|
||||||
|
.build(),
|
||||||
let request = GenerateContentRequest::builder().contents(prompt).build();
|
];
|
||||||
let response = gemini.generate_content(&request, "gemini-pro").await?;
|
|
||||||
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
|
let request = GenerateContentRequest::builder().contents(prompt).build();
|
||||||
|
let response = gemini.generate_content(&request, "gemini-pro").await?;
|
||||||
Ok(())
|
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
48
src/error.rs
48
src/error.rs
@@ -1,4 +1,5 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use tokio_util::codec::LinesCodecError;
|
use tokio_util::codec::LinesCodecError;
|
||||||
|
|
||||||
@@ -100,22 +101,33 @@ impl Error {
|
|||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the server-recommended retry delay if this error is a
|
||||||
|
/// [`Error::VertexError`] containing a `google.rpc.RetryInfo` detail entry.
|
||||||
|
pub fn retry_delay(&self) -> Option<Duration> {
|
||||||
|
match self {
|
||||||
|
Error::VertexError(e) => e.retry_delay(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn vertex_error(code: i32, status: &str, details: Option<Vec<serde_json::Value>>) -> Error {
|
||||||
|
Error::VertexError(types::VertexApiError {
|
||||||
|
code,
|
||||||
|
message: "test".to_string(),
|
||||||
|
status: status.to_string(),
|
||||||
|
details,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn vertex_error_retryable_classification() {
|
fn vertex_error_retryable_classification() {
|
||||||
let make_err = |code: i32, status: &str| {
|
let make_err = |code: i32, status: &str| vertex_error(code, status, None);
|
||||||
Error::VertexError(types::VertexApiError {
|
|
||||||
code,
|
|
||||||
message: "test".to_string(),
|
|
||||||
status: status.to_string(),
|
|
||||||
details: None,
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
// Retryable codes
|
// Retryable codes
|
||||||
assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable());
|
assert!(make_err(429, "TOO_MANY_REQUESTS").is_retryable());
|
||||||
@@ -155,6 +167,26 @@ mod tests {
|
|||||||
assert!(!make_err(reqwest::StatusCode::GATEWAY_TIMEOUT).is_retryable());
|
assert!(!make_err(reqwest::StatusCode::GATEWAY_TIMEOUT).is_retryable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_delegates_to_vertex_error() {
|
||||||
|
let err = vertex_error(
|
||||||
|
429,
|
||||||
|
"RESOURCE_EXHAUSTED",
|
||||||
|
Some(vec![serde_json::json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "15s"
|
||||||
|
})]),
|
||||||
|
);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(15)));
|
||||||
|
|
||||||
|
assert_eq!(Error::NoCandidatesError.retry_delay(), None);
|
||||||
|
let http_err = Error::HttpStatus {
|
||||||
|
status: reqwest::StatusCode::TOO_MANY_REQUESTS,
|
||||||
|
body: "error".to_string(),
|
||||||
|
};
|
||||||
|
assert_eq!(http_err.retry_delay(), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn other_errors_not_retryable() {
|
fn other_errors_not_retryable() {
|
||||||
assert!(!Error::NoCandidatesError.is_retryable());
|
assert!(!Error::NoCandidatesError.is_retryable());
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -7,7 +8,9 @@ pub struct VertexApiError {
|
|||||||
pub code: i32,
|
pub code: i32,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub status: String,
|
pub status: String,
|
||||||
pub details: Option<Vec<ErrorType>>,
|
/// Raw `google.rpc.*` detail entries, each tagged with an `@type` field. Kept as
|
||||||
|
/// untyped JSON so that unknown detail types never break deserialization.
|
||||||
|
pub details: Option<Vec<serde_json::Value>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl core::fmt::Display for VertexApiError {
|
impl core::fmt::Display for VertexApiError {
|
||||||
@@ -19,36 +22,240 @@ impl core::fmt::Display for VertexApiError {
|
|||||||
|
|
||||||
impl std::error::Error for VertexApiError {}
|
impl std::error::Error for VertexApiError {}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
impl VertexApiError {
|
||||||
pub struct Link {
|
/// Returns the recommended retry delay if provided in the error details.
|
||||||
pub description: String,
|
///
|
||||||
pub url: String,
|
/// When Gemini rejects a request due to quota limits (`429 RESOURCE_EXHAUSTED`),
|
||||||
|
/// its error details usually include a `google.rpc.RetryInfo` entry specifying
|
||||||
|
/// how long the client should wait before retrying (e.g. `"37s"` or `"1.5s"`).
|
||||||
|
///
|
||||||
|
/// Returns `None` if there are no error details, no `RetryInfo` entry is found,
|
||||||
|
/// or the `retryDelay` value is missing or cannot be parsed.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::time::Duration;
|
||||||
|
/// use gemini_rs::prelude::VertexApiError;
|
||||||
|
/// use serde_json::json;
|
||||||
|
///
|
||||||
|
/// let err = VertexApiError {
|
||||||
|
/// code: 429,
|
||||||
|
/// message: "Resource exhausted".into(),
|
||||||
|
/// status: "RESOURCE_EXHAUSTED".into(),
|
||||||
|
/// details: Some(vec![json!({
|
||||||
|
/// "@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
/// "retryDelay": "37s"
|
||||||
|
/// })]),
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// assert_eq!(err.retry_delay(), Some(Duration::from_secs(37)));
|
||||||
|
/// ```
|
||||||
|
pub fn retry_delay(&self) -> Option<Duration> {
|
||||||
|
self.details.as_deref()?.iter().find_map(|detail| {
|
||||||
|
let is_retry_info = detail
|
||||||
|
.get("@type")
|
||||||
|
.and_then(|t| t.as_str())
|
||||||
|
.is_some_and(is_retry_info_type);
|
||||||
|
if !is_retry_info {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
detail
|
||||||
|
.get("retryDelay")
|
||||||
|
.or_else(|| detail.get("retry_delay"))
|
||||||
|
.and_then(|d| d.as_str())
|
||||||
|
.and_then(parse_duration)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
fn is_retry_info_type(type_url: &str) -> bool {
|
||||||
#[serde(tag = "@type")]
|
type_url == "google.rpc.RetryInfo" || type_url.ends_with("/google.rpc.RetryInfo")
|
||||||
pub enum ErrorType {
|
|
||||||
#[serde(rename = "type.googleapis.com/google.rpc.ErrorInfo")]
|
|
||||||
ErrorInfo { metadata: ErrorInfoMetadata },
|
|
||||||
|
|
||||||
#[serde(rename = "type.googleapis.com/google.rpc.Help")]
|
|
||||||
Help { links: Vec<Link> },
|
|
||||||
|
|
||||||
#[serde(rename = "type.googleapis.com/google.rpc.BadRequest")]
|
|
||||||
BadRequest {
|
|
||||||
#[serde(rename = "fieldViolations")]
|
|
||||||
field_violations: Vec<FieldViolation>,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
/// Parses a protobuf JSON `Duration` string (`"<secs>s"` or `"<secs>.<frac>s"`).
|
||||||
pub struct ErrorInfoMetadata {
|
fn parse_duration(s: &str) -> Option<Duration> {
|
||||||
pub service: String,
|
let s = s.strip_suffix('s')?;
|
||||||
pub consumer: String,
|
let (secs_str, frac_str) = match s.split_once('.') {
|
||||||
|
Some((secs, frac)) => (secs, Some(frac)),
|
||||||
|
None => (s, None),
|
||||||
|
};
|
||||||
|
|
||||||
|
if secs_str.is_empty() || !secs_str.chars().all(|c| c.is_ascii_digit()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let secs: u64 = secs_str.parse().ok()?;
|
||||||
|
|
||||||
|
let nanos = match frac_str {
|
||||||
|
Some(frac) => {
|
||||||
|
if frac.is_empty() || frac.len() > 9 || !frac.chars().all(|c| c.is_ascii_digit()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let parsed: u32 = frac.parse().ok()?;
|
||||||
|
parsed * 10u32.pow(9 - frac.len() as u32)
|
||||||
|
}
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(Duration::new(secs, nanos))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[cfg(test)]
|
||||||
pub struct FieldViolation {
|
mod tests {
|
||||||
pub field: String,
|
use serde_json::json;
|
||||||
pub description: String,
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn retry_info_error(details: Vec<serde_json::Value>) -> VertexApiError {
|
||||||
|
VertexApiError {
|
||||||
|
code: 429,
|
||||||
|
message: "Quota exceeded".into(),
|
||||||
|
status: "RESOURCE_EXHAUSTED".into(),
|
||||||
|
details: Some(details),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserializes_unknown_detail_types() -> Result<(), serde_json::Error> {
|
||||||
|
let body = r#"{
|
||||||
|
"code": 429,
|
||||||
|
"message": "Resource exhausted",
|
||||||
|
"status": "RESOURCE_EXHAUSTED",
|
||||||
|
"details": [
|
||||||
|
{
|
||||||
|
"@type": "type.googleapis.com/google.rpc.QuotaFailure",
|
||||||
|
"violations": [{ "quotaMetric": "generate_content_requests" }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "37s"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}"#;
|
||||||
|
let err: VertexApiError = serde_json::from_str(body)?;
|
||||||
|
assert_eq!(err.details.as_ref().map(Vec::len), Some(2));
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(37)));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_whole_seconds() {
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "37s"
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(37)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_fractional_seconds() {
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "1.5s"
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_millis(1500)));
|
||||||
|
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "0.005s"
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_millis(5)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_without_details() {
|
||||||
|
let err = VertexApiError {
|
||||||
|
details: None,
|
||||||
|
..retry_info_error(vec![])
|
||||||
|
};
|
||||||
|
assert_eq!(err.retry_delay(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_without_retry_info() {
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.BadRequest"
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), None);
|
||||||
|
|
||||||
|
assert_eq!(retry_info_error(vec![]).retry_delay(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_with_unparsable_or_missing_retry_delay() {
|
||||||
|
let cases = [
|
||||||
|
"invalid",
|
||||||
|
"37", // missing 's' suffix
|
||||||
|
"-1s", // negative
|
||||||
|
"-1.5s", // negative fractional
|
||||||
|
"1.5.0s", // multiple decimals
|
||||||
|
".5s", // missing leading digits
|
||||||
|
"1.s", // missing fractional digits
|
||||||
|
"", // empty
|
||||||
|
"s", // only suffix
|
||||||
|
" 37s", // leading space
|
||||||
|
"37s ", // trailing space
|
||||||
|
"NaNs", // non-digit
|
||||||
|
"1e3s", // scientific notation
|
||||||
|
"1.1234567890s", // more than 9 fractional digits
|
||||||
|
];
|
||||||
|
|
||||||
|
for case in cases {
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": case
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), None, "expected None for: {case:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
let missing = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo"
|
||||||
|
})]);
|
||||||
|
assert_eq!(missing.retry_delay(), None);
|
||||||
|
|
||||||
|
let non_string = retry_info_error(vec![json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": 37
|
||||||
|
})]);
|
||||||
|
assert_eq!(non_string.retry_delay(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_with_multiple_details_entries() {
|
||||||
|
let err = retry_info_error(vec![
|
||||||
|
json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||||
|
"reason": "RATE_LIMIT_EXCEEDED"
|
||||||
|
}),
|
||||||
|
json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "42s"
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(42)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_with_multiple_retry_info_entries_skips_malformed() {
|
||||||
|
let err = retry_info_error(vec![
|
||||||
|
json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "malformed"
|
||||||
|
}),
|
||||||
|
json!({
|
||||||
|
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||||
|
"retryDelay": "20s"
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(20)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retry_delay_snake_case_and_short_type() {
|
||||||
|
let err = retry_info_error(vec![json!({
|
||||||
|
"@type": "google.rpc.RetryInfo",
|
||||||
|
"retry_delay": "10s"
|
||||||
|
})]);
|
||||||
|
assert_eq!(err.retry_delay(), Some(Duration::from_secs(10)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user