From 1af0b88881fe4a970c0a22373c23dd3be5b413d6 Mon Sep 17 00:00:00 2001 From: Craig Jellick Date: Thu, 13 Oct 2022 13:46:19 -0700 Subject: [PATCH] Make ErrBadName checkable via errors.Is() The function IsErrBadName says it is deprecated and directs the user to use errors.Is(), but that will never return true because this error is custom based on the tag value. This fixes that problem by implementing an Is() function on ErrBadName so that errors.Is() can properly identify the error as an ErrBadName. Usage can now be: errors.Is(err, &ErrBadName{}) Signed-off-by: Craig Jellick --- pkg/name/errors.go | 6 ++++++ pkg/name/errors_test.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/pkg/name/errors.go b/pkg/name/errors.go index 035e35069..35a25847f 100644 --- a/pkg/name/errors.go +++ b/pkg/name/errors.go @@ -28,6 +28,12 @@ func (e *ErrBadName) Error() string { return e.info } +// Is reports whether target is an error of type ErrBadName +func (e *ErrBadName) Is(target error) bool { + var berr *ErrBadName + return errors.As(target, &berr) +} + // newErrBadName returns a ErrBadName which returns the given formatted string from Error(). func newErrBadName(fmtStr string, args ...interface{}) *ErrBadName { return &ErrBadName{fmt.Sprintf(fmtStr, args...)} diff --git a/pkg/name/errors_test.go b/pkg/name/errors_test.go index 8ee6d4d8e..6ceb4590c 100644 --- a/pkg/name/errors_test.go +++ b/pkg/name/errors_test.go @@ -31,4 +31,8 @@ func TestBadName(t *testing.T) { if err.Error() != "could not parse reference: @@" { t.Errorf("Unexpected string: %v", err) } + //var cerr *ErrBadName + if !errors.Is(err, &ErrBadName{}) { + t.Errorf("Not an ErrBadName using errors.Is: %v", err) + } }