Skip to content

Commit e6acd96

Browse files
committed
WZ-42693 - Add last status code to error message
- Add last status code to error message
1 parent 9dfd949 commit e6acd96

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,16 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
816816

817817
// this means CheckRetry thought the request was a failure, but didn't
818818
// communicate why
819+
errMsg := fmt.Sprintf("%s %s giving up after %d attempt(s)", req.Method, redactURL(req.URL), attempt)
820+
if resp != nil {
821+
errMsg += fmt.Sprintf(" [last statusCode: %d]", resp.StatusCode)
822+
}
823+
819824
if err == nil {
820-
return nil, fmt.Errorf("%s %s giving up after %d attempt(s)",
821-
req.Method, redactURL(req.URL), attempt)
825+
return nil, fmt.Errorf(errMsg)
822826
}
823827

824-
return nil, fmt.Errorf("%s %s giving up after %d attempt(s): %w",
825-
req.Method, redactURL(req.URL), attempt, err)
828+
return nil, fmt.Errorf("%s: %w", errMsg, err)
826829
}
827830

828831
// Try to read the response body so we can reuse this connection.

client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,19 @@ func TestClient_Do_fails(t *testing.T) {
497497
url: ts.URL,
498498
name: "default_retry_policy",
499499
cr: DefaultRetryPolicy,
500-
err: "giving up after 3 attempt(s)",
500+
err: "giving up after 3 attempt(s) [last statusCode: 500]",
501501
},
502502
{
503503
url: serverUrlWithBasicAuth.String(),
504504
name: "default_retry_policy_url_with_basic_auth",
505505
cr: DefaultRetryPolicy,
506-
err: redactURL(serverUrlWithBasicAuth) + " giving up after 3 attempt(s)",
506+
err: redactURL(serverUrlWithBasicAuth) + " giving up after 3 attempt(s) [last statusCode: 500]",
507507
},
508508
{
509509
url: ts.URL,
510510
name: "error_propagated_retry_policy",
511511
cr: ErrorPropagatedRetryPolicy,
512-
err: "giving up after 3 attempt(s): unexpected HTTP status 500 Internal Server Error",
512+
err: "giving up after 3 attempt(s) [last statusCode: 500]: unexpected HTTP status 500 Internal Server Error",
513513
},
514514
}
515515

@@ -787,7 +787,7 @@ func TestClient_CheckRetry(t *testing.T) {
787787
t.Fatalf("CheckRetry called %d times, expected 1", called)
788788
}
789789

790-
if err.Error() != fmt.Sprintf("GET %s giving up after 2 attempt(s): retryError", ts.URL) {
790+
if err.Error() != fmt.Sprintf("GET %s giving up after 2 attempt(s) [last statusCode: 500]: retryError", ts.URL) {
791791
t.Fatalf("Expected retryError, got:%v", err)
792792
}
793793
}

0 commit comments

Comments
 (0)