Skip to content

Commit

Permalink
Merge pull request cloudflare#616 from cloudflare/error-helpers-for-m…
Browse files Browse the repository at this point in the history
…atching-slices

errors: Add helper methods for searching in slices
  • Loading branch information
jacobbednarz authored Apr 7, 2021
2 parents b94de39 + 02f7203 commit 6bf3fa1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,26 @@ func (e *APIRequestError) ClientError() bool {
func (e *APIRequestError) ClientRateLimited() bool {
return e.StatusCode == http.StatusTooManyRequests
}

// InternalErrorCodeIs returns a boolean whether or not the desired internal
// error code is present in `e.InternalErrorCodes`.
func (e *APIRequestError) InternalErrorCodeIs(code int) bool {
for _, errCode := range e.InternalErrorCodes() {
if errCode == code {
return true
}
}

return false
}

// ErrorMessageContains returns a boolean whether or not a substring exists in
// any of the `e.ErrorMessages` slice entries.
func (e *APIRequestError) ErrorMessageContains(s string) bool {
for _, errMsg := range e.ErrorMessages() {
if strings.Contains(errMsg, s) {
return true
}
}
return false
}
18 changes: 18 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,21 @@ func TestAPIRequestError_ClientRateLimited(t *testing.T) {
})
}
}

func TestAPIRequestError_InternalErrorCodeIs(t *testing.T) {
err := &APIRequestError{Errors: []ResponseInfo{
{Code: 1001},
{Code: 2001},
{Code: 3001},
}}
assert.Equal(t, err.InternalErrorCodeIs(3001), true)
}

func TestAPIRequestError_ErrorMessageContains(t *testing.T) {
err := &APIRequestError{Errors: []ResponseInfo{
{Message: "dns thing broke"},
{Message: "application thing broke"},
{Message: "network thing broke"},
}}
assert.Equal(t, err.ErrorMessageContains("application thing broke"), true)
}

0 comments on commit 6bf3fa1

Please sign in to comment.