Switch to strongly-typed errors

Removed the use of generic Error.
Declared and used specific error types

Change-Id: Iec68a6d037475222825efa8f3bba56fa5780cfa9
This commit is contained in:
Yasin, Siraj (SY495P) 2020-04-30 16:34:17 -05:00
parent a37a8800cc
commit 9530e88c97
2 changed files with 46 additions and 9 deletions

View File

@ -128,3 +128,44 @@ type ErrMissingPrimaryRepo struct {
func (e ErrMissingPrimaryRepo) Error() string { func (e ErrMissingPrimaryRepo) Error() string {
return "Current context manifest must have primary repository set" return "Current context manifest must have primary repository set"
} }
// ErrConflictingAuthOptions returned in case both token and username/password is set at same time
type ErrConflictingAuthOptions struct {
}
func (e ErrConflictingAuthOptions) Error() string {
return "you cannot specify token and username/password at the same time"
}
// ErrConflictingClusterOptions returned when both certificate-authority and
// insecure-skip-tls-verify is set at same time
type ErrConflictingClusterOptions struct {
}
func (e ErrConflictingClusterOptions) Error() string {
return "you cannot specify a certificate-authority and insecure-skip-tls-verify mode at the same time"
}
// ErrEmptyClusterName returned when empty cluster name is set
type ErrEmptyClusterName struct {
}
func (e ErrEmptyClusterName) Error() string {
return "you must specify a non-empty cluster name"
}
// ErrConflictingContextOptions returned when both context and --current is set at same time
type ErrConflictingContextOptions struct {
}
func (e ErrConflictingContextOptions) Error() string {
return "you cannot specify context and --current Flag at the same time"
}
// ErrEmptyContextName returned when empty context name is set
type ErrEmptyContextName struct {
}
func (e ErrEmptyContextName) Error() string {
return "you must specify a non-empty context name"
}

View File

@ -16,10 +16,7 @@ limitations under the License.
package config package config
// TODO(howell): Switch to strongly-typed errors
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
) )
@ -75,8 +72,7 @@ func (o *AuthInfoOptions) Validate() error {
// already had a user/pass and visa-versa. This could create bugs if a // already had a user/pass and visa-versa. This could create bugs if a
// user at first chooses one method, but later switches to another. // user at first chooses one method, but later switches to another.
if o.Token != "" && (o.Username != "" || o.Password != "") { if o.Token != "" && (o.Username != "" || o.Password != "") {
// TODO(howell): strongly type this error return ErrConflictingAuthOptions{}
return errors.New("you must specify either token or a username/password")
} }
if !o.EmbedCertData { if !o.EmbedCertData {
@ -98,11 +94,11 @@ func (o *AuthInfoOptions) Validate() error {
// Error when invalid value or incompatible choice of values given // Error when invalid value or incompatible choice of values given
func (o *ContextOptions) Validate() error { func (o *ContextOptions) Validate() error {
if !o.Current && o.Name == "" { if !o.Current && o.Name == "" {
return errors.New("you must specify a non-empty context name") return ErrEmptyContextName{}
} }
if o.Current && o.Name != "" { if o.Current && o.Name != "" {
return errors.New("you cannot specify context and --current Flag at the same time") return ErrConflictingContextOptions{}
} }
// If the user simply wants to change the current context, no further validation is needed // If the user simply wants to change the current context, no further validation is needed
@ -125,7 +121,7 @@ func (o *ContextOptions) Validate() error {
// Error when invalid value or incompatible choice of values given // Error when invalid value or incompatible choice of values given
func (o *ClusterOptions) Validate() error { func (o *ClusterOptions) Validate() error {
if o.Name == "" { if o.Name == "" {
return errors.New("you must specify a non-empty cluster name") return ErrEmptyClusterName{}
} }
err := ValidClusterType(o.ClusterType) err := ValidClusterType(o.ClusterType)
@ -134,7 +130,7 @@ func (o *ClusterOptions) Validate() error {
} }
if o.InsecureSkipTLSVerify && o.CertificateAuthority != "" { if o.InsecureSkipTLSVerify && o.CertificateAuthority != "" {
return errors.New("you cannot specify a certificate-authority and insecure-skip-tls-verify mode at the same time") return ErrConflictingClusterOptions{}
} }
if !o.EmbedCAData { if !o.EmbedCAData {