Skip to content

Commit

Permalink
Better error message when GitHub REST API fails (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello authored Jan 13, 2025
1 parent bb42387 commit c921c49
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/cmd/cli/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.)
Expand All @@ -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"`
Expand Down

0 comments on commit c921c49

Please sign in to comment.