Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in HTTP StatusCode error handling logic #16

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ func (client *Client) Do(req Req) (Res, error) {
log.Printf("[ERROR] HTTP Request failed with 'please try again'. Retrying.")
continue
}
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
}
// In case any previous conditions don't `continue`, return error
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
}
}

Expand Down
27 changes: 24 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,31 @@ func TestClientGetRetry(t *testing.T) {
client.AuthToken = "ABC"
client.LastRefresh = time.Now()

// Request should fail
gock.New(testURL).Get("/url_400").Reply(400)
_, err = client.Get("/url_400")
assert.Error(t, err)

// First request should fail, subsequent should be successful
gock.New(testURL).Get("/url").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
gock.New(testURL).Get("/url").Reply(200)
_, err = client.Get("/url")
gock.New(testURL).Get("/url_400_try_again").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
gock.New(testURL).Get("/url_400_try_again").Reply(200)
_, err = client.Get("/url_400_try_again")
assert.NoError(t, err)

// All requests should fail, as re-try counter is exceeded
gock.New(testURL).Get("/url_400_try_again_exceed_limit").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
gock.New(testURL).Get("/url_400_try_again_exceed_limit").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
gock.New(testURL).Get("/url_400_try_again_exceed_limit").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
gock.New(testURL).Get("/url_400_try_again_exceed_limit").Reply(400).BodyString(`{"error":{"category":"FRAMEWORK","messages":[{"description":"Search Service n.a. Please try again."}],"severity":"ERROR"}}`)
_, err = client.Get("/url_400_try_again_exceed_limit")
assert.Error(t, err)

// First three request should fail, final one should be successful
gock.New(testURL).Get("/url_510").Reply(510)
gock.New(testURL).Get("/url_510").Reply(510)
gock.New(testURL).Get("/url_510").Reply(510)
gock.New(testURL).Get("/url_510").Reply(200)
_, err = client.Get("/url_510")
assert.NoError(t, err)
}

Expand Down