Skip to content

Commit

Permalink
fix: pkg/errors is no longer maintained (#7440)
Browse files Browse the repository at this point in the history
* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>

* fix: pkg/errors is no longer maintained

Signed-off-by: Stephanie Palis <stephanie.palis@acquia.com>
  • Loading branch information
stephpalis committed Mar 18, 2022
1 parent fbb43b2 commit 051c7b8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 63 deletions.
73 changes: 30 additions & 43 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package errors

import (
"encoding/json"
"errors"
"fmt"
"io"

"github.com/pkg/errors"
)

// Externally visible error codes
Expand All @@ -24,30 +22,21 @@ const (
type ArgoError interface {
Error() string
Code() string
Message() string
JSON() []byte
StackTrace() errors.StackTrace
Format(s fmt.State, verb rune)
}

// argoerr is the internal implementation of an Argo error which wraps the error from pkg/errors
type argoerr struct {
code string
message string
stracer stackTracer
}

// stackTracer is interface for error types that have a stack trace
type stackTracer interface {
Error() string
StackTrace() errors.StackTrace
err error
}

// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(code string, message string) error {
err := errors.New(message)
return argoerr{code, message, err.(stackTracer)}
return argoerr{code, message, err}
}

// Errorf returns an error and formats according to a format specifier
Expand Down Expand Up @@ -85,8 +74,8 @@ func Wrap(err error, code string, message string) error {
if err == nil {
return nil
}
err = errors.Wrap(err, message)
return argoerr{code, message, err.(stackTracer)}
err = fmt.Errorf(message+": %w", err)
return argoerr{code, message, err}
}

// Cause returns the underlying cause of the error, if possible.
Expand All @@ -102,25 +91,41 @@ func Wrap(err error, code string, message string) error {
// investigation.
func Cause(err error) error {
if argoErr, ok := err.(argoerr); ok {
return errors.Cause(argoErr.stracer)
return unwrapCauseArgoErr(argoErr.err)
}
return errors.Cause(err)
return unwrapCause(err)
}

func (e argoerr) Error() string {
return e.message
func unwrapCauseArgoErr(err error) error {
innerErr := errors.Unwrap(err)
for innerErr != nil {
err = innerErr
innerErr = errors.Unwrap(err)
}
return err
}

func (e argoerr) Code() string {
return e.code
func unwrapCause(err error) error {
type causer interface {
Cause() error
}

for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}

func (e argoerr) Message() string {
func (e argoerr) Error() string {
return e.message
}

func (e argoerr) StackTrace() errors.StackTrace {
return e.stracer.StackTrace()
func (e argoerr) Code() string {
return e.code
}

func (e argoerr) JSON() []byte {
Expand All @@ -133,24 +138,6 @@ func (e argoerr) JSON() []byte {
return j
}

func (e argoerr) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
_, _ = io.WriteString(s, e.Error())
for _, pc := range e.StackTrace() {
fmt.Fprintf(s, "\n%+v", pc)
}
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}

// IsCode is a helper to determine if the error is of a specific code
func IsCode(code string, err error) bool {
if argoErr, ok := err.(argoerr); ok {
Expand Down
14 changes: 0 additions & 14 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import (
"fmt"
"testing"

pkgerr "github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/argoproj/argo-workflows/v3/errors"
)

// stackTracer is interface for error types that have a stack trace
type stackTracer interface {
StackTrace() pkgerr.StackTrace
}

// TestErrorf tests the initializer of error package
func TestErrorf(t *testing.T) {
err := errors.Errorf(errors.CodeInternal, "test internal")
Expand All @@ -38,17 +32,9 @@ func TestInternalError(t *testing.T) {
// Test wrapping errors
err = fmt.Errorf("random error")
intWrap := errors.InternalWrapError(err)
_ = intWrap.(stackTracer)
assert.Equal(t, "random error", intWrap.Error())
intWrap = errors.InternalWrapError(err, "different message")
_ = intWrap.(stackTracer)
assert.Equal(t, "different message", intWrap.Error())
intWrap = errors.InternalWrapErrorf(err, "hello %s", "world")
_ = intWrap.(stackTracer)
assert.Equal(t, "hello world", intWrap.Error())
}

func TestStackTrace(t *testing.T) {
err := errors.New("MYCODE", "my message")
assert.Contains(t, fmt.Sprintf("%+v", err), "errors_test.go")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ require (
github.com/klauspost/pgzip v1.2.5
github.com/minio/minio-go/v7 v7.0.23
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.32.1
Expand Down Expand Up @@ -145,6 +144,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rs/xid v1.2.1 // indirect
Expand Down
10 changes: 5 additions & 5 deletions util/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubeconfig

import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -10,7 +11,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
clientauthenticationapi "k8s.io/client-go/pkg/apis/clientauthentication"
"k8s.io/client-go/plugin/pkg/client/auth/exec"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -127,7 +127,7 @@ func GetAuthString(in *restclient.Config, explicitKubeConfigPath string) (string

func GetBasicAuthToken(in *restclient.Config) (string, error) {
if in == nil {
return "", errors.Errorf("RestClient can't be nil")
return "", fmt.Errorf("RestClient can't be nil")
}

return encodeBasicAuthToken(in.Username, in.Password), nil
Expand All @@ -140,7 +140,7 @@ func GetBearerToken(in *restclient.Config, explicitKubeConfigPath string) (strin
}

if in == nil {
return "", errors.Errorf("RestClient can't be nil")
return "", fmt.Errorf("RestClient can't be nil")
}
if in.ExecProvider != nil {
tc, err := in.TransportConfig()
Expand Down Expand Up @@ -194,7 +194,7 @@ func GetBearerToken(in *restclient.Config, explicitKubeConfigPath string) (strin
return strings.TrimPrefix(token, "Bearer "), nil
}
}
return "", errors.Errorf("could not find a token")
return "", fmt.Errorf("could not find a token")
}

/*https://pkg.go.dev/k8s.io/client-go@v0.20.4/pkg/apis/clientauthentication#Cluster
Expand Down Expand Up @@ -281,7 +281,7 @@ func RefreshTokenIfExpired(restConfig *restclient.Config, explicitPath, curentTo
if timestr != "" {
t, err := time.Parse(time.RFC3339, timestr)
if err != nil {
return "", errors.Errorf("Invalid expiry date in Kubeconfig. %v", err)
return "", fmt.Errorf("Invalid expiry date in Kubeconfig. %v", err)
}
if time.Now().After(t) {
err = RefreshAuthToken(restConfig)
Expand Down

0 comments on commit 051c7b8

Please sign in to comment.