diff --git a/pkg/download/download.go b/pkg/download/download.go index b8bb59f40e..7e16029592 100644 --- a/pkg/download/download.go +++ b/pkg/download/download.go @@ -6,6 +6,7 @@ package download import ( + "bytes" "context" "encoding/base64" "fmt" @@ -238,7 +239,12 @@ func download(req *http.Request, options *downloadOptions) (data []byte, err err } if resp.StatusCode != http.StatusOK { - return data, retry.ExpectedError(fmt.Errorf("failed to download config, received %d", resp.StatusCode)) + // try to read first 32 bytes of the response body + // to provide more context in case of error + data, _ = io.ReadAll(io.LimitReader(resp.Body, 32)) //nolint:errcheck // as error already happened, we don't care much about this one + data = bytes.ToValidUTF8(data, nil) + + return data, retry.ExpectedError(fmt.Errorf("failed to download config, status code %d, body %q", resp.StatusCode, string(data))) } data, err = io.ReadAll(resp.Body) diff --git a/pkg/download/download_test.go b/pkg/download/download_test.go index 3b0040e210..da0169aa29 100644 --- a/pkg/download/download_test.go +++ b/pkg/download/download_test.go @@ -54,6 +54,7 @@ func TestDownload(t *testing.T) { w.Write([]byte("ZGF0YQ==")) //nolint:errcheck case "/404": w.WriteHeader(http.StatusNotFound) + fmt.Fprintln(w, "not found") default: w.WriteHeader(http.StatusInternalServerError) } @@ -109,7 +110,7 @@ func TestDownload(t *testing.T) { name: "failure 404", path: "/404", opts: []download.Option{download.WithTimeout(2 * time.Second)}, - expectedError: "failed to download config, received 404", + expectedError: "failed to download config, status code 404, body \"not found\\n\"", }, { name: "retry endpoint change",