Skip to content

Commit 07b3870

Browse files
committed
feat!: use NotFound feature-test in IsNotFound()
This is a BREAKING CHANGE as it no longer strictly matches only this ErrNotFound type but any type implementing interface{ NotFound() bool }. Ref: ipld/go-ipld-prime#494
1 parent 530b600 commit 07b3870

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

merkledag.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package format
22

33
import (
44
"context"
5-
"errors"
65

76
cid "github.com/ipfs/go-cid"
87
)
@@ -28,6 +27,8 @@ func (e ErrNotFound) Error() string {
2827

2928
// Is allows to check whether any error is of this ErrNotFound type.
3029
// Do not use this directly, but rather errors.Is(yourError, ErrNotFound).
30+
// For maximum compatibility you should prefer IsNotFound() instead as it will
31+
// also match other compatible NotFound error types.
3132
func (e ErrNotFound) Is(err error) bool {
3233
switch err.(type) {
3334
case ErrNotFound:
@@ -42,10 +43,14 @@ func (e ErrNotFound) NotFound() bool {
4243
return true
4344
}
4445

45-
// IsNotFound returns if the given error is or wraps an ErrNotFound
46-
// (equivalent to errors.Is(err, ErrNotFound{}))
46+
// IsNotFound returns true if the error is a ErrNotFound. As it uses a
47+
// feature-test, it is also compatible with other NotFound error types,
48+
// including github.com/ipld/go-ipld-prime/storage#ErrNotFound.
4749
func IsNotFound(err error) bool {
48-
return errors.Is(err, ErrNotFound{})
50+
if nf, ok := err.(interface{ NotFound() bool }); ok {
51+
return nf.NotFound()
52+
}
53+
return false
4954
}
5055

5156
// Either a node or an error.

0 commit comments

Comments
 (0)