Skip to content

Commit

Permalink
Avoid panic when download request fails at connection stage
Browse files Browse the repository at this point in the history
`RawBody()` may return nil if the connection failed (e.g. connection
refused).
  • Loading branch information
hansmi committed Jan 5, 2024
1 parent 5e6acf6 commit a2279ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/client/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (c *Client) download(ctx context.Context, w io.Writer, url string, expectDi

resp, err := req.Get(url)

if resp != nil {
defer multierr.AppendInvoke(&err, multierr.Close(resp.RawBody()))
if !(resp == nil || resp.RawBody() == nil) {
defer multierr.AppendFunc(&err, resp.RawBody().Close)
}

if err := convertError(err, resp); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func TestDownload(t *testing.T) {
Message: `418`,
},
},
{
name: "connection error",
setup: func(t *testing.T, transport *httpmock.MockTransport) {
transport.RegisterResponder(http.MethodGet, "/conn/error",
httpmock.ConnectionFailure)
},
url: "/conn/error",
wantErr: cmpopts.AnyError,
},
} {
t.Run(tc.name, func(t *testing.T) {
transport := newMockTransport(t)
Expand Down

0 comments on commit a2279ed

Please sign in to comment.