Handle HTTP download errors

This commit is contained in:
Giovanni Tirloni 2018-04-26 18:59:30 -03:00
parent 2650d59499
commit 413d13c411
No known key found for this signature in database
GPG Key ID: 566242517457C940
2 changed files with 22 additions and 0 deletions

View File

@ -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
@ -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)

View File

@ -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"))