Updated common/download.go to handle when a connection error happens (response is nil), and reformatted the error that's returned when an HTTP error occurs.

This commit is contained in:
Ali Rizvi-Santiago 2018-07-28 18:36:57 -05:00
parent fe9e1bc9af
commit 71e43d0b7f
1 changed files with 25 additions and 7 deletions

View File

@ -278,12 +278,24 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
}
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("[DEBUG] (download) Error making HTTP HEAD request: %s", err.Error())
if err != nil || resp == nil {
if resp == nil {
log.Printf("[DEBUG] (download) HTTP connection error: %s", err.Error())
} else if resp.StatusCode >= 400 && resp.StatusCode < 600 {
log.Printf("[DEBUG] (download) Non-successful HTTP status code (%s) while making HEAD request: %s", resp.Status, err.Error())
} else {
log.Printf("[DEBUG] (download) Error making HTTP HEAD request (%s): %s", resp.Status, err.Error())
}
} else {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
// If the HEAD request succeeded, then attempt to set the range
// query if we can.
if resp.Header.Get("Accept-Ranges") == "bytes" {
if fi, err := dst.Stat(); err == nil {
if _, err = dst.Seek(0, os.SEEK_END); err == nil {
@ -293,6 +305,7 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
}
}
}
} else {
log.Printf("[DEBUG] (download) Unexpected HTTP response during HEAD request: %s", resp.Status)
}
@ -302,12 +315,17 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
req.Method = "GET"
resp, err = httpClient.Do(req)
if err != nil {
return err
} else {
if resp.StatusCode >= 400 && resp.StatusCode < 600 {
return fmt.Errorf("Error making HTTP GET request: %s", resp.Status)
if err == nil && (resp.StatusCode >= 400 && resp.StatusCode < 600) {
return fmt.Errorf("Error making HTTP GET request: %s", resp.Status)
} else if err != nil {
if resp == nil {
return fmt.Errorf("HTTP connection error: %s", err.Error())
} else if resp.StatusCode >= 400 && resp.StatusCode < 600 {
return fmt.Errorf("HTTP %s error: %s", resp.Status, err.Error())
}
return fmt.Errorf("HTTP error: %s", err.Error())
}
d.total = d.current + uint64(resp.ContentLength)