Skip to content

Commit

Permalink
Use errors.As and errors.Is for safe checks and casts
Browse files Browse the repository at this point in the history
  • Loading branch information
markusthoemmes committed Feb 12, 2021
1 parent 758624e commit cc71b76
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
20 changes: 11 additions & 9 deletions pkg/errors/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package errors

import (
"errors"
"net/http"
"strings"

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/kn/commands/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package plugin

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/plugin/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package plugin

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cc71b76

Please sign in to comment.