Skip to content

Commit e82c69e

Browse files
authored
fix: Return clearer error when team name empty or not set (#1354)
Right now the error users get when the team is not set is `failed to configure quota monitor: failed to get usage: 404 Not Found`. The recent CLI change in cloudquery/cloudquery#15119 will ensure that the team should mostly get set, but there can still be cases where users are in multiple teams and forget to run `cloudquery switch`. In such a case, a friendlier error would be helpful.
1 parent f5c1bbe commit e82c69e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

premium/usage.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package premium
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math/rand"
78
"net/http"
9+
"os"
810
"sync/atomic"
911
"time"
1012

@@ -166,15 +168,6 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
166168
op(u)
167169
}
168170

169-
// Set team name from configuration if not provided
170-
if u.teamName == "" {
171-
teamName, err := config.GetValue("team")
172-
if err != nil {
173-
return nil, fmt.Errorf("failed to get team name from config: %w", err)
174-
}
175-
u.teamName = teamName
176-
}
177-
178171
// Create a default api client if none was provided
179172
if u.apiClient == nil {
180173
tokenClient := auth.NewTokenClient()
@@ -192,6 +185,20 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl
192185
u.apiClient = ac
193186
}
194187

188+
// Set team name from configuration if not provided
189+
if u.teamName == "" {
190+
teamName, err := config.GetValue("team")
191+
if errors.Is(err, os.ErrNotExist) {
192+
return nil, fmt.Errorf("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch <team>`", err)
193+
} else if err != nil {
194+
return nil, fmt.Errorf("failed to get team name from config: %w", err)
195+
}
196+
if teamName == "" {
197+
return nil, fmt.Errorf("team name not set. Hint: use `cloudquery switch <team>`")
198+
}
199+
u.teamName = teamName
200+
}
201+
195202
u.backgroundUpdater()
196203

197204
return u, nil

0 commit comments

Comments
 (0)