Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message when GitHub REST API fails #951

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
lionello marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading