Skip to content

Commit

Permalink
[v2.8] Small refactor (error messages) (#374)
Browse files Browse the repository at this point in the history
* small refactor for requested changes

* added error to error message

* used ErrorContains in tests
  • Loading branch information
enrichman authored May 31, 2024
1 parent 1d80411 commit cf0d6e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 12 additions & 12 deletions cmd/kubectl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func runCredential(ctx *cli.Context) error {
}
server := ctx.String("server")
if server == "" {
return fmt.Errorf("name of rancher server is required")
return errors.New("name of rancher server is required")
}
url, err := url2.Parse(server)
if err != nil {
Expand All @@ -151,7 +151,7 @@ func runCredential(ctx *cli.Context) error {
}
userID := ctx.String("user")
if userID == "" {
return fmt.Errorf("user-id is required")
return errors.New("user-id is required")
}
clusterID := ctx.String("cluster")

Expand Down Expand Up @@ -254,7 +254,7 @@ func loadCachedCredential(ctx *cli.Context, key string) (*config.ExecCredential,
func lookupServerConfig(ctx *cli.Context) (*config.ServerConfig, error) {
server := ctx.String("server")
if server == "" {
return nil, fmt.Errorf("name of rancher server is required")
return nil, errors.New("name of rancher server is required")
}

cf, err := loadConfig(ctx)
Expand Down Expand Up @@ -283,7 +283,7 @@ func cacheCredential(ctx *cli.Context, cred *config.ExecCredential, id string) e

server := ctx.String("server")
if server == "" {
return fmt.Errorf("name of rancher server is required")
return errors.New("name of rancher server is required")
}

cf, err := loadConfig(ctx)
Expand Down Expand Up @@ -524,21 +524,21 @@ type TypedProvider interface {
}

func getAuthProviders(server string) ([]TypedProvider, error) {
authProviders := fmt.Sprintf(authProviderURL, server)
customPrint(authProviders)
authProvidersURL := fmt.Sprintf(authProviderURL, server)
customPrint(authProvidersURL)

response, err := request(http.MethodGet, authProviders, nil)
response, err := request(http.MethodGet, authProvidersURL, nil)
if err != nil {
return nil, err
}

if !gjson.ValidBytes(response) {
return nil, errors.New("invalid JSON input")
return nil, fmt.Errorf("invalid JSON response from %s", authProvidersURL)
}

data := gjson.GetBytes(response, "data").Array()

supportedProviders := []TypedProvider{}
var supportedProviders []TypedProvider
for _, provider := range data {
providerType := provider.Get("type").String()

Expand All @@ -556,7 +556,7 @@ func getAuthProviders(server string) ([]TypedProvider, error) {

err = json.Unmarshal([]byte(provider.Raw), typedProvider)
if err != nil {
return nil, err
return nil, fmt.Errorf("attempting to decode the auth provider of type %s: %w", providerType, err)
}
supportedProviders = append(supportedProviders, typedProvider)
}
Expand All @@ -567,7 +567,7 @@ func getAuthProviders(server string) ([]TypedProvider, error) {

func selectAuthProvider(authProviders []TypedProvider, providerType string) (TypedProvider, error) {
if len(authProviders) == 0 {
return nil, fmt.Errorf("no auth provider configured")
return nil, errors.New("no auth provider configured")
}

// if providerType was specified, look for it
Expand Down Expand Up @@ -609,7 +609,7 @@ func selectAuthProvider(authProviders []TypedProvider, providerType string) (Typ
return authProviders[providerIndex], nil
}

return nil, fmt.Errorf("invalid auth provider")
return nil, errors.New("invalid auth provider")
}

func generateKey() (string, error) {
Expand Down
6 changes: 4 additions & 2 deletions cmd/kubectl_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ func Test_getAuthProviders(t *testing.T) {
{
name: "json error",
server: setupServer(`hnjskjnksnj`),
expectedErr: "invalid JSON input",
expectedErr: "invalid JSON response from",
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(tc.server.Close)

got, err := getAuthProviders(tc.server.URL)

if tc.expectedErr != "" {
assert.EqualError(t, err, tc.expectedErr)
assert.ErrorContains(t, err, tc.expectedErr)
assert.Nil(t, got)
} else {
assert.Equal(t, tc.expectedProviders, got)
Expand Down

0 comments on commit cf0d6e3

Please sign in to comment.