feat: capture HTTP Retry-After header in Error::retry_delay() (#21)
All checks were successful
Rust / check (pull_request) Successful in 2m41s
All checks were successful
Rust / check (pull_request) Successful in 2m41s
Error::HttpStatus gains a retry_after field, filled from the response's Retry-After header (delay-seconds form) before the body is consumed. Error::retry_delay() now returns it for HttpStatus errors, so a 429 or 503 with a non-Vertex body still exposes the server's retry hint. Closes #21
This commit is contained in:
42
src/error.rs
42
src/error.rs
@@ -21,6 +21,9 @@ pub enum Error {
|
||||
HttpStatus {
|
||||
status: reqwest::StatusCode,
|
||||
body: String,
|
||||
/// The delay from the response's `Retry-After` header, if present and in
|
||||
/// delay-seconds form.
|
||||
retry_after: Option<Duration>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -34,7 +37,7 @@ impl Display for Error {
|
||||
Error::VertexError(e) => write!(f, "Vertex error: {e}"),
|
||||
Error::NoCandidatesError => write!(f, "No candidates returned for the prompt"),
|
||||
Error::EventSourceError(e) => write!(f, "EventSource error: {e}"),
|
||||
Error::HttpStatus { status, body } => write!(f, "HTTP status {status}: {body}"),
|
||||
Error::HttpStatus { status, body, .. } => write!(f, "HTTP status {status}: {body}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,11 +105,14 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the server-recommended retry delay if this error is a
|
||||
/// [`Error::VertexError`] containing a `google.rpc.RetryInfo` detail entry.
|
||||
/// Returns the server-recommended retry delay, if any.
|
||||
///
|
||||
/// - `VertexError`: the delay from a `google.rpc.RetryInfo` detail entry.
|
||||
/// - `HttpStatus`: the delay from the response's `Retry-After` header.
|
||||
pub fn retry_delay(&self) -> Option<Duration> {
|
||||
match self {
|
||||
Error::VertexError(e) => e.retry_delay(),
|
||||
Error::HttpStatus { retry_after, .. } => *retry_after,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -125,6 +131,14 @@ mod tests {
|
||||
})
|
||||
}
|
||||
|
||||
fn http_status_error(status: reqwest::StatusCode, retry_after: Option<Duration>) -> Error {
|
||||
Error::HttpStatus {
|
||||
status,
|
||||
body: "error".to_string(),
|
||||
retry_after,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vertex_error_retryable_classification() {
|
||||
let make_err = |code: i32, status: &str| vertex_error(code, status, None);
|
||||
@@ -150,10 +164,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn http_status_retryable_classification() {
|
||||
let make_err = |status: reqwest::StatusCode| Error::HttpStatus {
|
||||
status,
|
||||
body: "error".to_string(),
|
||||
};
|
||||
let make_err = |status: reqwest::StatusCode| http_status_error(status, None);
|
||||
|
||||
assert!(make_err(reqwest::StatusCode::TOO_MANY_REQUESTS).is_retryable());
|
||||
assert!(make_err(reqwest::StatusCode::INTERNAL_SERVER_ERROR).is_retryable());
|
||||
@@ -180,11 +191,18 @@ mod tests {
|
||||
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]
|
||||
fn retry_delay_returns_http_status_retry_after() {
|
||||
let err = http_status_error(
|
||||
reqwest::StatusCode::TOO_MANY_REQUESTS,
|
||||
Some(Duration::from_secs(30)),
|
||||
);
|
||||
assert_eq!(err.retry_delay(), Some(Duration::from_secs(30)));
|
||||
|
||||
let err = http_status_error(reqwest::StatusCode::TOO_MANY_REQUESTS, None);
|
||||
assert_eq!(err.retry_delay(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user