Improve Modrinth command errors

This commit is contained in:
comp500 2022-06-08 23:36:15 +01:00
parent 65688cf2b1
commit b8be3784f6
3 changed files with 21 additions and 7 deletions

View File

@ -144,7 +144,7 @@ func installMod(mod Mod, pack core.Pack) error {
latestVersion, err := getLatestVersion(mod.ID, pack) latestVersion, err := getLatestVersion(mod.ID, pack)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to get latest version: %v", err)
} }
if latestVersion.ID == "" { if latestVersion.ID == "" {
return errors.New("mod is not available for this Minecraft version (use the acceptable-game-versions option to accept more) or mod loader") return errors.New("mod is not available for this Minecraft version (use the acceptable-game-versions option to accept more) or mod loader")
@ -249,12 +249,12 @@ func installVersion(mod Mod, version Version, pack core.Pack) error {
func installVersionById(versionId string, pack core.Pack) error { func installVersionById(versionId string, pack core.Pack) error {
version, err := fetchVersion(versionId) version, err := fetchVersion(versionId)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to fetch version %s: %v", versionId, err)
} }
mod, err := fetchMod(version.ModID) mod, err := fetchMod(version.ModID)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to fetch mod %s: %v", version.ModID, err)
} }
return installVersion(mod, version, pack) return installVersion(mod, version, pack)

View File

@ -3,6 +3,7 @@ package modrinth
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"io/ioutil" "io/ioutil"
@ -194,7 +195,11 @@ func getLatestVersion(modID string, pack core.Pack) (Version, error) {
} }
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
return Version{}, errors.New("couldn't find mod: " + modID) return Version{}, fmt.Errorf("mod not found (for URL %v)", baseUrl)
}
if resp.StatusCode != 200 {
return Version{}, fmt.Errorf("invalid response status %v for URL %v", resp.Status, baseUrl)
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -253,7 +258,11 @@ func fetchMod(modID string) (Mod, error) {
} }
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
return mod, errors.New("couldn't find mod: " + modID) return mod, fmt.Errorf("mod not found (for URL %v)", modrinthApiUrl+"mod/"+modID)
}
if resp.StatusCode != 200 {
return mod, fmt.Errorf("invalid response status %v for URL %v", resp.Status, modrinthApiUrl+"mod/"+modID)
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -283,7 +292,11 @@ func fetchVersion(versionId string) (Version, error) {
} }
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
return version, errors.New("couldn't find version: " + versionId) return version, fmt.Errorf("version not found (for URL %v)", modrinthApiUrl+"version/"+versionId)
}
if resp.StatusCode != 200 {
return version, fmt.Errorf("invalid response status %v for URL %v", resp.Status, modrinthApiUrl+"version/"+versionId)
} }
defer resp.Body.Close() defer resp.Body.Close()

View File

@ -2,6 +2,7 @@ package modrinth
import ( import (
"errors" "errors"
"fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/packwiz/packwiz/core" "github.com/packwiz/packwiz/core"
@ -45,7 +46,7 @@ func (u mrUpdater) CheckUpdate(mods []core.Mod, mcVersion string, pack core.Pack
newVersion, err := getLatestVersion(data.ModID, pack) newVersion, err := getLatestVersion(data.ModID, pack)
if err != nil { if err != nil {
results[i] = core.UpdateCheck{Error: err} results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)}
continue continue
} }