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

feat: show first 32 bytes of response body on download error #7938

Merged
merged 1 commit into from
Nov 8, 2023
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: 7 additions & 1 deletion pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package download

import (
"bytes"
"context"
"encoding/base64"
"fmt"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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",
Expand Down