Merge pull request #6207 from gtirloni/issue-6203
[WIP] Handle HTTP download errors
This commit is contained in:
commit
7c46e3d89c
|
@ -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)
|
||||
|
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in New Issue