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

View File

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