Skip to content

Commit 1a0eb6c

Browse files
committed
fix: Correctly handle error codes
1 parent b62bbea commit 1a0eb6c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

nsd/download/download.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/ioutil"
77
"math"
88
"net/http"
9-
"os"
109
"path"
1110
"strconv"
1211
"strings"
@@ -62,12 +61,12 @@ func File(url string) (filePath string, err error) {
6261
if err != nil {
6362
return
6463
}
64+
65+
defer resp.Body.Close()
66+
6567
if resp.StatusCode != http.StatusOK {
66-
fmt.Println(resp.Status)
67-
os.Exit(1)
68-
// exit if not ok
68+
return "", fmt.Errorf("URL '%s' returned error code %s", url, resp.Status)
6969
}
70-
defer resp.Body.Close()
7170

7271
// the Header "Content-Length" will let us know
7372
// the total file size to download

nsd/download/download_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ func TestDownloadZip(t *testing.T) {
5454
t.Errorf("File() returned path to file, but file does not exist")
5555
}
5656
}
57+
58+
func TestDownloadInvalidFile(t *testing.T) {
59+
router := mux.NewRouter()
60+
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./testdata/")))
61+
62+
server := httptest.NewServer(router)
63+
defer server.Close()
64+
65+
_, err := File(server.URL + "/notexist.zip")
66+
if err == nil {
67+
t.Errorf("File() did not error")
68+
}
69+
}

0 commit comments

Comments
 (0)