From b8be3784f674427eb45ed4b5e696f582aa5f18ac Mon Sep 17 00:00:00 2001 From: comp500 Date: Wed, 8 Jun 2022 23:36:15 +0100 Subject: [PATCH] Improve Modrinth command errors --- modrinth/install.go | 6 +++--- modrinth/modrinth.go | 19 ++++++++++++++++--- modrinth/updater.go | 3 ++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/modrinth/install.go b/modrinth/install.go index 796fdb7..e9c32dc 100644 --- a/modrinth/install.go +++ b/modrinth/install.go @@ -144,7 +144,7 @@ func installMod(mod Mod, pack core.Pack) error { latestVersion, err := getLatestVersion(mod.ID, pack) if err != nil { - return err + return fmt.Errorf("failed to get latest version: %v", err) } 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") @@ -249,12 +249,12 @@ func installVersion(mod Mod, version Version, pack core.Pack) error { func installVersionById(versionId string, pack core.Pack) error { version, err := fetchVersion(versionId) if err != nil { - return err + return fmt.Errorf("failed to fetch version %s: %v", versionId, err) } mod, err := fetchMod(version.ModID) if err != nil { - return err + return fmt.Errorf("failed to fetch mod %s: %v", version.ModID, err) } return installVersion(mod, version, pack) diff --git a/modrinth/modrinth.go b/modrinth/modrinth.go index 6ac55d1..8141621 100644 --- a/modrinth/modrinth.go +++ b/modrinth/modrinth.go @@ -3,6 +3,7 @@ package modrinth import ( "encoding/json" "errors" + "fmt" "github.com/spf13/viper" "golang.org/x/exp/slices" "io/ioutil" @@ -194,7 +195,11 @@ func getLatestVersion(modID string, pack core.Pack) (Version, error) { } 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() @@ -253,7 +258,11 @@ func fetchMod(modID string) (Mod, error) { } 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() @@ -283,7 +292,11 @@ func fetchVersion(versionId string) (Version, error) { } 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() diff --git a/modrinth/updater.go b/modrinth/updater.go index 04a00bc..e927ccb 100644 --- a/modrinth/updater.go +++ b/modrinth/updater.go @@ -2,6 +2,7 @@ package modrinth import ( "errors" + "fmt" "github.com/mitchellh/mapstructure" "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) if err != nil { - results[i] = core.UpdateCheck{Error: err} + results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)} continue }