-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(error): surface retry-after header #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ pub enum GwsError { | |
| reason: String, | ||
| /// For `accessNotConfigured` errors: the GCP console URL to enable the API. | ||
| enable_url: Option<String>, | ||
| /// Seconds to wait before retrying, when the API returned a Retry-After header. | ||
| retry_after_seconds: Option<u64>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the field is correctly added to the JSON output, it is currently missing from the human-readable For a CLI tool, surfacing the retry hint in the terminal output (e.g., |
||
| }, | ||
|
|
||
| #[error("{0}")] | ||
|
|
@@ -71,6 +73,7 @@ impl GwsError { | |
| message, | ||
| reason, | ||
| enable_url, | ||
| retry_after_seconds, | ||
| } => { | ||
| let mut error_obj = json!({ | ||
| "code": code, | ||
|
|
@@ -80,6 +83,9 @@ impl GwsError { | |
| if let Some(url) = enable_url { | ||
| error_obj["enable_url"] = json!(url); | ||
| } | ||
| if let Some(seconds) = retry_after_seconds { | ||
| error_obj["retry_after_seconds"] = json!(seconds); | ||
| } | ||
| json!({ "error": error_obj }) | ||
| } | ||
| GwsError::Validation(msg) => json!({ | ||
|
|
@@ -125,6 +131,7 @@ mod tests { | |
| message: "Not Found".to_string(), | ||
| reason: "notFound".to_string(), | ||
| enable_url: None, | ||
| retry_after_seconds: None, | ||
| }; | ||
| assert_eq!(err.exit_code(), GwsError::EXIT_CODE_API); | ||
| } | ||
|
|
@@ -185,6 +192,7 @@ mod tests { | |
| message: "Not Found".to_string(), | ||
| reason: "notFound".to_string(), | ||
| enable_url: None, | ||
| retry_after_seconds: None, | ||
| }; | ||
| let json = err.to_json(); | ||
| assert_eq!(json["error"]["code"], 404); | ||
|
|
@@ -236,6 +244,7 @@ mod tests { | |
| message: "Gmail API has not been used in project 549352339482 before or it is disabled.".to_string(), | ||
| reason: "accessNotConfigured".to_string(), | ||
| enable_url: Some("https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=549352339482".to_string()), | ||
| retry_after_seconds: None, | ||
| }; | ||
| let json = err.to_json(); | ||
| assert_eq!(json["error"]["code"], 403); | ||
|
|
@@ -253,10 +262,25 @@ mod tests { | |
| message: "API not enabled.".to_string(), | ||
| reason: "accessNotConfigured".to_string(), | ||
| enable_url: None, | ||
| retry_after_seconds: None, | ||
| }; | ||
| let json = err.to_json(); | ||
| assert_eq!(json["error"]["code"], 403); | ||
| assert_eq!(json["error"]["reason"], "accessNotConfigured"); | ||
| assert!(json["error"]["enable_url"].is_null()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_error_to_json_retry_after_seconds() { | ||
| let err = GwsError::Api { | ||
| code: 429, | ||
| message: "Rate limit exceeded.".to_string(), | ||
| reason: "rateLimitExceeded".to_string(), | ||
| enable_url: None, | ||
| retry_after_seconds: Some(17), | ||
| }; | ||
| let json = err.to_json(); | ||
| assert_eq!(json["error"]["code"], 429); | ||
| assert_eq!(json["error"]["retry_after_seconds"], 17); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calculating the retry delay using
SystemTime::now()is susceptible to clock skew between the client and the server. Per RFC 7231 Section 7.1.1.2, the recipient SHOULD use the value of theDateheader field from the response as the reference time for calculating delta-seconds from an HTTP-date. If the client's clock is significantly ahead of the server's, this calculation could result in a0delay, leading to immediate retries that continue to fail.