diff --git a/fastly/errors.go b/fastly/errors.go index af3314775..cc82f2c45 100644 --- a/fastly/errors.go +++ b/fastly/errors.go @@ -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 } // ErrorObject is a single error. @@ -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 } @@ -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() }