diff --git a/errors.go b/errors.go index 8d5d792754..2200480c60 100644 --- a/errors.go +++ b/errors.go @@ -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 +} diff --git a/errors_test.go b/errors_test.go index 2648953a88..4e5b6e5440 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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) +}