Skip to content

Commit

Permalink
fix: Improve error messages when plugin download fails (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanschaaf authored Nov 10, 2023
1 parent 88f303c commit 4d664cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
19 changes: 11 additions & 8 deletions managedplugin/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ func DownloadPluginFromHub(ctx context.Context, authToken, localPath, team, name
return fmt.Errorf("failed to get plugin url: %w", err)
}
defer downloadURL.Body.Close()
if downloadURL.StatusCode == http.StatusNotFound {
return fmt.Errorf("failed to get plugin url for %v %v/%v@%v: plugin version not found. If you're trying to use a paid plugin you'll need to run `cloudquery login` first", typ, team, name, version)
}
if downloadURL.StatusCode == http.StatusTooManyRequests {
return fmt.Errorf("failed to get plugin url for %v %v/%v@%v: too many requests. Try logging in via `cloudquery login` to increase rate limits", typ, team, name, version)
}
if downloadURL.StatusCode != http.StatusFound {
return fmt.Errorf("failed to get plugin url for %v %v/%v@%v: unexpected status code %v", typ, team, name, version, downloadURL.StatusCode)
switch downloadURL.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusFound:
// we allow these status codes, but typically expect a redirect (302)
case http.StatusUnauthorized:
return fmt.Errorf("unauthorized. Try logging in via `cloudquery login`")
case http.StatusNotFound:
return fmt.Errorf("failed to download plugin %v %v/%v@%v: plugin version not found. If you're trying to use a private plugin you'll need to run `cloudquery login` first", typ, team, name, version)
case http.StatusTooManyRequests:
return fmt.Errorf("too many download requests. Try logging in via `cloudquery login` to increase rate limits")
default:
return fmt.Errorf("failed to download plugin %v %v/%v@%v: unexpected status code %v", typ, team, name, version, downloadURL.StatusCode)
}
location, ok := downloadURL.Header["Location"]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion managedplugin/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestDownloadPluginFromCloudQueryHub(t *testing.T) {
wantErr bool
typ PluginType
}{
{testName: "should download test plugin from cloudquery registry", team: "cloudquery", plugin: "azuredevops", version: "v3.0.12", typ: PluginSource},
{testName: "should download test plugin from cloudquery registry", team: "cloudquery", plugin: "aws", version: "v22.18.0", typ: PluginSource},
}
for _, tc := range cases {
t.Run(tc.testName, func(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions managedplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,38 @@ func TestManagedPluginCloudQuery(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
cfg := Config{
Name: "azuredevops",
Name: "awspricing",
Registry: RegistryCloudQuery,
Path: "cloudquery/azuredevops",
Path: "cloudquery/awspricing",
Version: "v3.0.12",
}
clients, err := NewClients(ctx, PluginSource, []Config{cfg}, WithDirectory(tmpDir), WithNoSentry())
if err != nil {
t.Fatal(err)
}
testClient := clients.ClientByName("azuredevops")
testClient := clients.ClientByName("awspricing")
if testClient == nil {
t.Fatal("azuredevops client not found")
t.Fatal("awspricing client not found")
}
if err := clients.Terminate(); err != nil {
t.Fatal(err)
}
localPath := filepath.Join(tmpDir, "plugins", PluginSource.String(), "cloudquery", "azuredevops", cfg.Version, "plugin")
localPath := filepath.Join(tmpDir, "plugins", PluginSource.String(), "cloudquery", "awspricing", cfg.Version, "plugin")
localPath = WithBinarySuffix(localPath)
cfg = Config{
Name: "azuredevops",
Name: "awspricing",
Registry: RegistryLocal,
Path: localPath,
Version: "v3.0.12",
Version: cfg.Version,
}

clients, err = NewClients(ctx, PluginSource, []Config{cfg}, WithDirectory(tmpDir), WithNoSentry())
if err != nil {
t.Fatal(err)
}
testClient = clients.ClientByName("azuredevops")
testClient = clients.ClientByName("awspricing")
if testClient == nil {
t.Fatal("azuredevops client not found")
t.Fatal("awspricing client not found")
}
if err := clients.Terminate(); err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 4d664cc

Please sign in to comment.