diff --git a/pkg/errors/factory.go b/pkg/errors/factory.go index 80f03d4816..e25de50084 100644 --- a/pkg/errors/factory.go +++ b/pkg/errors/factory.go @@ -15,6 +15,7 @@ package errors import ( + "errors" "net/http" "strings" @@ -52,17 +53,17 @@ func GetError(err error) error { case isNoRouteToHostError(err): return newNoRouteToHost(err.Error()) default: - apiStatus, ok := err.(api_errors.APIStatus) - if !ok { + var errAPIStatus api_errors.APIStatus + if !errors.As(err, &errAPIStatus) { return err } - if apiStatus.Status().Details == nil { + if errAPIStatus.Status().Details == nil { return err } var knerr *KNError - if isCRDError(apiStatus) { - knerr = newInvalidCRD(apiStatus.Status().Details.Group) - knerr.Status = apiStatus + if isCRDError(errAPIStatus) { + knerr = newInvalidCRD(errAPIStatus.Status().Details.Group) + knerr.Status = errAPIStatus return knerr } return err @@ -71,8 +72,9 @@ func GetError(err error) error { // IsForbiddenError returns true if given error can be converted to API status and of type forbidden access else false func IsForbiddenError(err error) bool { - if status, ok := err.(api_errors.APIStatus); ok { - return status.Status().Code == int32(http.StatusForbidden) + var errAPIStatus api_errors.APIStatus + if !errors.As(err, &errAPIStatus) { + return false } - return false + return errAPIStatus.Status().Code == int32(http.StatusForbidden) } diff --git a/pkg/kn/commands/service/update.go b/pkg/kn/commands/service/update.go index ab2a18c7bb..836cb1095b 100644 --- a/pkg/kn/commands/service/update.go +++ b/pkg/kn/commands/service/update.go @@ -87,7 +87,8 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command { var baseRevision *servingv1.Revision if !cmd.Flags().Changed("image") && editFlags.LockToDigest { baseRevision, err = client.GetBaseRevision(service) - if _, ok := err.(*clientservingv1.NoBaseRevisionError); ok { + var errNoBaseRevision clientservingv1.NoBaseRevisionError + if errors.As(err, &errNoBaseRevision) { fmt.Fprintf(cmd.OutOrStdout(), "Warning: No revision found to update image digest") } } diff --git a/pkg/kn/config/config.go b/pkg/kn/config/config.go index 16d0124248..9a5f62a7f4 100644 --- a/pkg/kn/config/config.go +++ b/pkg/kn/config/config.go @@ -15,6 +15,7 @@ package config import ( + "errors" "fmt" "os" "path/filepath" @@ -106,7 +107,7 @@ func BootstrapConfig() error { bootstrapFlagSet.ParseErrorsWhitelist = flag.ParseErrorsWhitelist{UnknownFlags: true} // wokeignore:rule=whitelist // TODO(#1031) bootstrapFlagSet.Usage = func() {} err := bootstrapFlagSet.Parse(os.Args) - if err != nil && err != flag.ErrHelp { + if err != nil && !errors.Is(err, flag.ErrHelp) { return err } diff --git a/pkg/kn/plugin/manager.go b/pkg/kn/plugin/manager.go index 42dde116ed..0ab5c654f9 100644 --- a/pkg/kn/plugin/manager.go +++ b/pkg/kn/plugin/manager.go @@ -15,6 +15,7 @@ package plugin import ( + "errors" "fmt" "io/ioutil" "os" @@ -449,7 +450,7 @@ func findInDirOrPath(name string, dir string, lookupInPath bool) (string, error) // Found in path return path, nil } - if execErr, ok := err.(*exec.Error); !ok || execErr.Unwrap() != exec.ErrNotFound { + if errors.Is(err, exec.ErrNotFound) { return "", fmt.Errorf("error for looking up %s in path: %w", name, err) } } diff --git a/pkg/kn/plugin/verify.go b/pkg/kn/plugin/verify.go index 5fbe952c0f..3c0aea04b7 100644 --- a/pkg/kn/plugin/verify.go +++ b/pkg/kn/plugin/verify.go @@ -15,6 +15,7 @@ package plugin import ( + "errors" "fmt" "io" "io/ioutil" @@ -83,7 +84,7 @@ func verifyPath(path string, seenPlugins map[string]string, eaw VerificationErro // Verify that plugin actually exists fileInfo, err := os.Stat(path) if err != nil { - if err == os.ErrNotExist { + if errors.Is(err, os.ErrNotExist) { return eaw.AddError("cannot find plugin in %s", path) } return eaw.AddError("cannot stat %s: %v", path, err)