Skip to content

Commit

Permalink
Handle HTTP download errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Tirloni committed Apr 26, 2018
1 parent 2650d59 commit 413d13c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
}
}
}
} else if err != nil || (resp.StatusCode >= 400 && resp.StatusCode < 600) {
return fmt.Errorf("%s", resp.Status)
}

// Set the request to GET now, and redo the query to download
Expand All @@ -298,6 +300,8 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
resp, err = httpClient.Do(req)
if err != nil {
return err
} else if err != nil || (resp.StatusCode >= 400 && resp.StatusCode < 600) {
return fmt.Errorf("%s", resp.Status)
}

d.total = d.current + uint64(resp.ContentLength)
Expand Down
18 changes: 18 additions & 0 deletions common/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ func TestDownloadClient_checksumNoDownload(t *testing.T) {
}
}

func TestDownloadClient_notFound(t *testing.T) {
tf, _ := ioutil.TempFile("", "packer")
tf.Close()
os.Remove(tf.Name())

ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root")))
defer ts.Close()

client := NewDownloadClient(&DownloadConfig{
Url: ts.URL + "/not-found.txt",
TargetPath: tf.Name(),
})

if _, err := client.Get(); err == nil {
t.Fatal("should error")
}
}

func TestDownloadClient_resume(t *testing.T) {
tf, _ := ioutil.TempFile("", "packer")
tf.Write([]byte("w"))
Expand Down

0 comments on commit 413d13c

Please sign in to comment.