Skip to content

Commit

Permalink
fix: Correctly handle error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Nov 2, 2020
1 parent b62bbea commit 1a0eb6c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 4 additions & 5 deletions nsd/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"math"
"net/http"
"os"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -62,12 +61,12 @@ func File(url string) (filePath string, err error) {
if err != nil {
return
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
fmt.Println(resp.Status)
os.Exit(1)
// exit if not ok
return "", fmt.Errorf("URL '%s' returned error code %s", url, resp.Status)
}
defer resp.Body.Close()

// the Header "Content-Length" will let us know
// the total file size to download
Expand Down
13 changes: 13 additions & 0 deletions nsd/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ func TestDownloadZip(t *testing.T) {
t.Errorf("File() returned path to file, but file does not exist")
}
}

func TestDownloadInvalidFile(t *testing.T) {
router := mux.NewRouter()
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./testdata/")))

server := httptest.NewServer(router)
defer server.Close()

_, err := File(server.URL + "/notexist.zip")
if err == nil {
t.Errorf("File() did not error")
}
}

0 comments on commit 1a0eb6c

Please sign in to comment.