Skip to content

Commit 22d3367

Browse files
authored
feat: improve httputil error messages (#624)
* add attempt count to the timeout error message so it's clear the timeout is due to the total time accumulated during retries. * add lastFailure to the timeout error message so we can get a clear reason for the retries (or, at the very least, the last retry). Otherwise, the error message obscures the real reason(s) for the retries and potentially misleads users into thinking that the errors could be e.g. network related (more typical of a timeout) rather than other types of errors (e.g. "tls: failed to verify certificate", etc.)
1 parent d20eff9 commit 22d3367

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

httputil/httputil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func get(url, auth string) (*http.Response, error) {
113113

114114
nextTryAt := RetryClock.Now().Add(waitFor)
115115
if nextTryAt.After(deadline) {
116-
return nil, fmt.Errorf("unable to complete request to %s within %v", url, MaxRequestDuration)
116+
return nil, fmt.Errorf("unable to complete %d requests to %s within %v. Most recent failure: %s", attempt+1, url, MaxRequestDuration, lastFailure)
117117
}
118118
if attempt < MaxRetries {
119119
RetryClock.Sleep(waitFor)

httputil/httputil_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net/http"
66
"strconv"
7+
"strings"
78
"testing"
89
"time"
910
)
@@ -219,10 +220,13 @@ func TestDeadlineExceeded(t *testing.T) {
219220
t.Fatal("Expected request to fail with code 500")
220221
}
221222

222-
wanted := "could not fetch http://bar: unable to complete request to http://bar within 8s"
223+
wantedPrefix := "could not fetch http://bar: unable to complete "
224+
wantedSuffix := " requests to http://bar within 8s. Most recent failure: HTTP 500"
225+
223226
got := err.Error()
224-
if wanted != got {
225-
t.Fatalf("Expected error %q, but got %q", wanted, got)
227+
sameError := strings.HasPrefix(got, wantedPrefix) && strings.HasSuffix(got, wantedSuffix)
228+
if !sameError {
229+
t.Fatalf("Expected error %q, but got %q", wantedPrefix+"???"+wantedSuffix, got)
226230
}
227231
}
228232

0 commit comments

Comments
 (0)