Skip to content
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

feat(errors): expose rate limit headers #447

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ type HTTPError struct {
Errors []*ErrorObject `mapstructure:"errors"`
// StatusCode is the HTTP status code (2xx-5xx).
StatusCode int
// RateLimitRemaining is the number of API requests remaining in the current
// rate limit window. A `nil` value indicates the API returned no value for
// the associated Fastly-RateLimit-Remaining response header.
RateLimitRemaining *int
// RateLimitReset is the time at which the current rate limit window resets,
// as a Unix timestamp. A `nil` value indicates the API returned no value for
// the associated Fastly-RateLimit-Reset response header.
RateLimitReset *int
Integralist marked this conversation as resolved.
Show resolved Hide resolved
}

// ErrorObject is a single error.
Expand All @@ -344,6 +352,13 @@ func NewHTTPError(resp *http.Response) *HTTPError {
var e HTTPError
e.StatusCode = resp.StatusCode

if v, err := strconv.Atoi(resp.Header.Get("Fastly-RateLimit-Remaining")); err == nil {
e.RateLimitRemaining = &v
}
if v, err := strconv.Atoi(resp.Header.Get("Fastly-RateLimit-Reset")); err == nil {
e.RateLimitReset = &v
}

if resp.Body == nil {
return &e
}
Expand Down Expand Up @@ -438,6 +453,13 @@ func (e *HTTPError) Error() string {
}
}

if e.RateLimitRemaining != nil {
fmt.Fprintf(&b, "\n RateLimitRemaining: %v", *e.RateLimitRemaining)
}
if e.RateLimitReset != nil {
fmt.Fprintf(&b, "\n RateLimitReset: %v", *e.RateLimitReset)
}

return b.String()
}

Expand Down