From b3e01473f32d3bf74e9471a7708b2cb39a48c01f Mon Sep 17 00:00:00 2001 From: Lionello Lunesu Date: Sat, 11 Jan 2025 13:21:50 -0800 Subject: [PATCH] Better error message when GitHub REST API fails --- src/cmd/cli/command/version.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cmd/cli/command/version.go b/src/cmd/cli/command/version.go index 4b16f9c2..8d44f1e1 100644 --- a/src/cmd/cli/command/version.go +++ b/src/cmd/cli/command/version.go @@ -3,11 +3,12 @@ package command import ( "context" "encoding/json" - "errors" + "fmt" "os" "strings" "github.com/DefangLabs/defang/src/pkg/http" + "github.com/DefangLabs/defang/src/pkg/term" "golang.org/x/mod/semver" ) @@ -36,6 +37,12 @@ func GetCurrentVersion() string { return version } +type githubError struct { + Message string + Status string + DocumentationUrl string +} + func GetLatestVersion(ctx context.Context) (string, error) { // Anonymous API request to GitHub are rate limited to 60 requests per hour per IP. // Check whether the user has set a GitHub token to increase the rate limit. (Copied from the install script.) @@ -53,9 +60,14 @@ func GetLatestVersion(ctx context.Context) (string, error) { } defer resp.Body.Close() if resp.StatusCode != 200 { + term.Debug(resp.Header) // The primary rate limit for unauthenticated requests is 60 requests per hour, per IP. // The API returns a 403 status code when the rate limit is exceeded. - return "", errors.New(resp.Status) + githubError := githubError{Message: resp.Status} + if err := json.NewDecoder(resp.Body).Decode(&githubError); err != nil { + term.Debugf("Failed to decode GitHub response: %v", err) + } + return "", fmt.Errorf("error fetching release info from GitHub: %s", githubError.Message) } var release struct { TagName string `json:"tag_name"`