Skip to content

Commit

Permalink
Merge pull request #17 from carolynvs/handle-http-error
Browse files Browse the repository at this point in the history
Handle http error during download
  • Loading branch information
carolynvs authored Apr 6, 2022
2 parents 7c11ea4 + 860df83 commit 1e72d20
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/downloads/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func DownloadToGopathBin(opts DownloadOptions) error {
return fmt.Errorf("could not resolve %s: %w", src, err)
}
defer r.Body.Close()
if r.StatusCode >= 400 {
return fmt.Errorf("error downloading %s (%d): %s", src, r.StatusCode, r.Status)
}

f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions pkg/downloads/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package downloads

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"
)

func TestDownloadToGopathBin(t *testing.T) {
t.Run("not found", func(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer svr.Close()

t.Run("not found", func(t *testing.T) {
opts := DownloadOptions{
UrlTemplate: svr.URL,
}
err := DownloadToGopathBin(opts)
require.Error(t, err)
assert.Contains(t, err.Error(), "404 Not Found")
})
})
}

0 comments on commit 1e72d20

Please sign in to comment.