Merge pull request #6207 from gtirloni/issue-6203

[WIP] Handle HTTP download errors
This commit is contained in:
M. Marsh 2018-05-08 15:56:06 -07:00 committed by GitHub
commit 7c46e3d89c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -173,6 +173,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"))