Skip to content

Commit

Permalink
Add error messages for failed version lookups (when nodejs.org return…
Browse files Browse the repository at this point in the history
…s partial/no results. Resolves #1064.
  • Loading branch information
coreybutler committed Nov 22, 2023
1 parent 4854ee0 commit 7af9934
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 11 additions & 3 deletions src/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions src/nvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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]
}
Expand Down
5 changes: 5 additions & 0 deletions src/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7af9934

Please sign in to comment.