Skip to content

Commit 082778c

Browse files
committed
SUMO-228138: Adding ErrorHandler in retryble client to handle http error codes.
1 parent 8a6b441 commit 082778c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

sumologic/sumologic_client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"net/http"
1212
"net/url"
13+
"strconv"
1314
"time"
1415

1516
"github.com/hashicorp/go-retryablehttp"
@@ -63,7 +64,7 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces
6364
func logRequestAndResponse(req *http.Request, resp *http.Response) {
6465
var maskedHeader = req.Header.Clone()
6566
maskedHeader.Set("Authorization", "xxxxxxxxxxx")
66-
log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [StatusCode=%s]\n", req.Method, req.URL, maskedHeader, resp.Status)
67+
log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [Status=%s] [Number of Retries=%s]\n", req.Method, req.URL, maskedHeader, resp.Status, resp.Header.Get("numberOfRetries"))
6768
}
6869

6970
func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) {
@@ -328,12 +329,18 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
328329
return d, nil
329330
}
330331

332+
func ErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
333+
log.Printf("[DEBUG] Resquest Failed after %d number of retries with Response: [%s]", numTries, resp.Status)
334+
resp.Header.Add("numberOfRetries", strconv.Itoa(numTries))
335+
return resp, err
336+
}
337+
331338
func NewClient(accessID, accessKey, authJwt, environment, base_url string, admin bool) (*Client, error) {
332339
retryClient := retryablehttp.NewClient()
333340
retryClient.RetryMax = 10
334341
// Disable DEBUG logs (https://github.com/hashicorp/go-retryablehttp/issues/31)
335342
retryClient.Logger = nil
336-
343+
retryClient.ErrorHandler = ErrorHandler
337344
client := Client{
338345
AccessID: accessID,
339346
AccessKey: accessKey,

0 commit comments

Comments
 (0)