Skip to content

Commit a0f6687

Browse files
cjellickimjasonh
andauthored
Make ErrBadName checkable via errors.Is() (#1462)
* 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 <craig@acorn.io> * Update pkg/name/errors_test.go Co-authored-by: Jason Hall <jason@chainguard.dev> Signed-off-by: Craig Jellick <craig@acorn.io> Co-authored-by: Jason Hall <jason@chainguard.dev>
1 parent 7268da0 commit a0f6687

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

pkg/name/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ func (e *ErrBadName) Error() string {
2828
return e.info
2929
}
3030

31+
// Is reports whether target is an error of type ErrBadName
32+
func (e *ErrBadName) Is(target error) bool {
33+
var berr *ErrBadName
34+
return errors.As(target, &berr)
35+
}
36+
3137
// newErrBadName returns a ErrBadName which returns the given formatted string from Error().
3238
func newErrBadName(fmtStr string, args ...interface{}) *ErrBadName {
3339
return &ErrBadName{fmt.Sprintf(fmtStr, args...)}

pkg/name/errors_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ func TestBadName(t *testing.T) {
3131
if err.Error() != "could not parse reference: @@" {
3232
t.Errorf("Unexpected string: %v", err)
3333
}
34+
if !errors.Is(err, &ErrBadName{}) {
35+
t.Errorf("Not an ErrBadName using errors.Is: %v", err)
36+
}
3437
}

0 commit comments

Comments
 (0)