From 7af9934eaf8fa8d505549fd05e046b5e8e1e6b52 Mon Sep 17 00:00:00 2001 From: Corey Butler <770982+coreybutler@users.noreply.github.com> Date: Wed, 22 Nov 2023 10:41:38 -0600 Subject: [PATCH] Add error messages for failed version lookups (when nodejs.org returns partial/no results. Resolves #1064. --- src/go.mod | 1 - src/go.sum | 2 -- src/node/node.go | 14 +++++++++++--- src/nvm.go | 17 ++++++++++++++--- src/web/web.go | 5 +++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/go.mod b/src/go.mod index 1a658b8a..2be2b0b9 100644 --- a/src/go.mod +++ b/src/go.mod @@ -8,7 +8,6 @@ require ( github.com/blang/semver v3.5.1+incompatible github.com/coreybutler/go-fsutil v1.2.0 github.com/coreybutler/go-where v1.0.2 - github.com/gonutz/w32/v2 v2.8.1 github.com/olekukonko/tablewriter v0.0.5 github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d golang.org/x/sys v0.1.0 diff --git a/src/go.sum b/src/go.sum index 0220cc4a..5fd56b9b 100644 --- a/src/go.sum +++ b/src/go.sum @@ -4,8 +4,6 @@ github.com/coreybutler/go-fsutil v1.2.0 h1:kbm62NSofawglUppEOhpHC3NDf/J7ZpguBirB github.com/coreybutler/go-fsutil v1.2.0/go.mod h1:B+6ufEkkRZgFwyR2sHEVG9dMzVBU3GbyGyYmCq7YkEk= github.com/coreybutler/go-where v1.0.2 h1:Omit67KeTtKpvSJjezVxnVD4qMtvlXDlItiKpVCdcl4= github.com/coreybutler/go-where v1.0.2/go.mod h1:IqV4saJiDXdNJURfTfVRywDHvY1IG5F+GXb2kmnmEe8= -github.com/gonutz/w32/v2 v2.8.1 h1:fTAzhg35iCGWqroImF18+A8GbI3MdXcTYuhvpBpd5CM= -github.com/gonutz/w32/v2 v2.8.1/go.mod h1:MgtHx0AScDVNKyB+kjyPder4xIi3XAcHS6LDDU2DmdE= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= diff --git a/src/node/node.go b/src/node/node.go index 88169466..77a0b842 100644 --- a/src/node/node.go +++ b/src/node/node.go @@ -2,14 +2,15 @@ package node import ( "encoding/json" + "fmt" "io/ioutil" "nvm/arch" "nvm/file" "nvm/web" + "os" "os/exec" "regexp" "strings" - "os" // "../semver" "github.com/blang/semver" @@ -208,13 +209,20 @@ func GetAvailable() ([]string, []string, []string, []string, []string, map[strin // Check the service to make sure the version is available text := web.GetRemoteTextFile(url) + if len(text) == 0 { + fmt.Println("Error retrieving version list: \"" + url + "\" returned blank results. This can happen when the remote file is being updated. Please try again in a few minutes.") + os.Exit(0) + } // Parse var data = make([]map[string]interface{}, 0) - json.Unmarshal([]byte(text), &data) + err := json.Unmarshal([]byte(text), &data) + if err != nil { + fmt.Printf("Error retrieving versions from \"%s\": %v", url, err.Error()) + os.Exit(1) + } for _, element := range data { - var version = element["version"].(string)[1:] all = append(all, version) diff --git a/src/nvm.go b/src/nvm.go index f0899d69..9bcf5966 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -1138,8 +1138,12 @@ func checkLocalEnvironment() { } } else { if fileInfo.Mode()&os.ModeSymlink != 0 { - targetPath, _ := os.Readlink(symlink) - targetFileInfo, _ := os.Lstat(targetPath) + targetPath, err := os.Readlink(symlink) + if err != nil { + fmt.Println(err) + } + + targetFileInfo, err := os.Lstat(targetPath) if !targetFileInfo.Mode().IsDir() { problems = append(problems, "NVM_SYMLINK is a symlink linking to a file instead of a directory.") @@ -1312,7 +1316,14 @@ func getLatest() string { } func getLTS() string { - _, ltsList, _, _, _, _ := node.GetAvailable() + all, ltsList, current, stable, unstable, npm := node.GetAvailable() + fmt.Println(all) + fmt.Println(ltsList) + fmt.Println(current) + fmt.Println(stable) + fmt.Println(unstable) + fmt.Println(npm) + // _, ltsList, _, _, _, _ := node.GetAvailable() // ltsList has already been numerically sorted return ltsList[0] } diff --git a/src/web/web.go b/src/web/web.go index 55e75c61..259adeeb 100644 --- a/src/web/web.go +++ b/src/web/web.go @@ -321,6 +321,11 @@ func GetRemoteTextFile(url string) string { os.Exit(1) } + if response.StatusCode != 200 { + fmt.Printf("Error retrieving \"%s\": HTTP Status %v\n", url, response.StatusCode) + os.Exit(0) + } + defer response.Body.Close() contents, readerr := ioutil.ReadAll(response.Body)