Skip to content

Commit

Permalink
Merge pull request #302 from Scalingo/feat/301/handle_too_many_requests
Browse files Browse the repository at this point in the history
feat(status code): handle too many requests exception
  • Loading branch information
ipfaze authored Feb 6, 2023
2 parents a82d55a + d7bef4c commit 54f56e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## To Be Released
* feat(status code): handle too many requests exception

## 6.1.0

Expand Down
16 changes: 16 additions & 0 deletions http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type (
Errors map[string][]string `json:"errors"`
}

TooManyRequestsError struct {
ErrMessage string `json:"error"`
Code string `json:"code"`
}

APIError struct {
Error string `json:"error"`
}
Expand Down Expand Up @@ -94,6 +99,10 @@ func (err UnprocessableEntity) Error() string {
return strings.Join(errArray, "\n")
}

func (err TooManyRequestsError) Error() string {
return fmt.Sprintf("429 Too Many Requests → %v", err.ErrMessage)
}

func (err ForbiddenError) Error() string {
return fmt.Sprintf("Request forbidden (403): %v", err.Err)
}
Expand Down Expand Up @@ -141,6 +150,13 @@ func NewRequestFailedError(res *http.Response, req *APIRequest) error {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: unprocessableError, Req: req}
case 429:
var tooManyRequestsError TooManyRequestsError
err := parseJSON(res, &tooManyRequestsError)
if err != nil {
return err
}
return &RequestFailedError{Code: res.StatusCode, APIError: tooManyRequestsError, Req: req}
case 500:
return &RequestFailedError{Code: res.StatusCode, APIError: errgo.New("server internal error - our team has been notified"), Req: req}
case 503:
Expand Down

0 comments on commit 54f56e9

Please sign in to comment.