Skip to content

Commit

Permalink
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 13.04.2022, Version 1.6.3

- add retryable errors

## 18.01.2022, Version 1.6.2

- add new alert source types
25 changes: 22 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,18 @@ func (aerr *GenericAPIError) Error() string {
return fmt.Sprintf("Error occurred with status code: %d, error code: %s, message: %s", aerr.Status, aerr.Code, aerr.Message)
}

// RetryableAPIError describes retryable API response error e.g. too many requests
type RetryableAPIError struct {
error
Status int `json:"status"`
Message string `json:"message"`
Code string `json:"code"`
}

func (aerr *RetryableAPIError) Error() string {
return fmt.Sprintf("Error occurred with status code: %d, error code: %s, message: %s", aerr.Status, aerr.Code, aerr.Message)
}

// GenericCountResponse describes generic resources count response
type GenericCountResponse struct {
Count int `json:"count"`
@@ -142,7 +154,7 @@ func WithRetry(retryCount int, retryWaitTime time.Duration, retryMaxWaitTime tim
}

// getGenericAPIError extract API response error
func getGenericAPIError(response *resty.Response, expectedStatusCode ...int) *GenericAPIError {
func getGenericAPIError(response *resty.Response, expectedStatusCode ...int) error {
if !intSliceContains(expectedStatusCode, response.StatusCode()) {
out := &GenericAPIError{}
err := json.Unmarshal(response.Body(), out)
@@ -153,8 +165,15 @@ func getGenericAPIError(response *resty.Response, expectedStatusCode ...int) *Ge
Message: "An error occurred",
}
}
if out.Message == "" {
return nil
if out.Status == 0 {
out.Status = response.StatusCode()
}
if retryCondition(response, out) {
return &RetryableAPIError{
Status: response.StatusCode(),
Code: out.Code,
Message: out.Message,
}
}
return out
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ilert

// Version package version
const Version = "v1.6.2"
const Version = "v1.6.3"

0 comments on commit 7b2c689

Please sign in to comment.