Reduce cyclomatic complexity for cache download

Update `TestCacheExtractorRemoteServerTimedOut` since the timeout message can be
different from time to time for the same reason.

Extract the error handling for the response status code to a common func between
`upload` and `getCache` func.
parent 55d5b061
package helpers
import (
"fmt"
"net/http"
"os"
"path/filepath"
......@@ -62,17 +61,7 @@ func (c *CacheArchiverCommand) upload() error {
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
err = fmt.Errorf("received: %s", resp.Status)
if resp.StatusCode/100 == 5 {
err = retryableErr{err: err}
}
return err
}
return nil
return retryOnServerError(resp)
}
func (c *CacheArchiverCommand) Execute(*cli.Context) {
......
package helpers
import (
"fmt"
"io"
"io/ioutil"
"net/http"
......@@ -44,24 +43,13 @@ func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) {
func (c *CacheExtractorCommand) download() error {
os.MkdirAll(filepath.Dir(c.File), 0700)
resp, err := c.getClient().Get(c.URL)
resp, err := c.getCache()
if err != nil {
return retryableErr{err: err}
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return os.ErrNotExist
} else if resp.StatusCode/100 != 2 {
err = fmt.Errorf("received: %s", resp.Status)
if resp.StatusCode/100 == 5 {
err = retryableErr{err: err}
}
return err
}
defer resp.Body.Close()
upToDate, date := checkIfUpToDate(c.File, resp)
if upToDate {
logrus.Infoln(filepath.Base(c.File), "is up to date")
......@@ -95,6 +83,20 @@ func (c *CacheExtractorCommand) download() error {
return nil
}
func (c *CacheExtractorCommand) getCache() (*http.Response, error) {
resp, err := c.getClient().Get(c.URL)
if err != nil {
return nil, retryableErr{err: err}
}
if resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
return nil, os.ErrNotExist
}
return resp, retryOnServerError(resp)
}
func (c *CacheExtractorCommand) Execute(context *cli.Context) {
log.SetRunnerFormatter()
......
......@@ -134,7 +134,7 @@ func TestCacheExtractorRemoteServerTimedOut(t *testing.T) {
assert.Panics(t, func() {
cmd.Execute(nil)
})
assert.Contains(t, buf.String(), "net/http: request canceled (Client.Timeout")
assert.Contains(t, buf.String(), "Client.Timeout")
_, err := os.Stat(cacheExtractorTestArchivedFile)
assert.Error(t, err)
......
package helpers
import (
"fmt"
"net/http"
"time"
"github.com/sirupsen/logrus"
......@@ -39,3 +41,22 @@ func (r *retryHelper) doRetry(handler func() error) error {
return err
}
// retryOnServerError will take the response and check if the the error should
// be of type retryableErr or not. When the status code is of 5xx it will be a
// retryableErr.
func retryOnServerError(resp *http.Response) error {
if resp.StatusCode/100 == 2 {
return nil
}
resp.Body.Close()
err := fmt.Errorf("received: %s", resp.Status)
if resp.StatusCode/100 == 5 {
err = retryableErr{err: err}
}
return err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment