From cd97e053b5db8fdb3ec3d0ecb416afe34516517d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 16 Feb 2023 17:13:21 +1100 Subject: [PATCH] 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: https://github.com/ipld/go-ipld-prime/pull/494 --- merkledag.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/merkledag.go b/merkledag.go index cb2ff3c..5b1a429 100644 --- a/merkledag.go +++ b/merkledag.go @@ -2,7 +2,6 @@ package format import ( "context" - "errors" cid "github.com/ipfs/go-cid" ) @@ -28,6 +27,8 @@ func (e ErrNotFound) Error() string { // Is allows to check whether any error is of this ErrNotFound type. // Do not use this directly, but rather errors.Is(yourError, ErrNotFound). +// For maximum compatibility you should prefer IsNotFound() instead as it will +// also match other compatible NotFound error types. func (e ErrNotFound) Is(err error) bool { switch err.(type) { case ErrNotFound: @@ -42,10 +43,14 @@ func (e ErrNotFound) NotFound() bool { return true } -// IsNotFound returns if the given error is or wraps an ErrNotFound -// (equivalent to errors.Is(err, ErrNotFound{})) +// IsNotFound returns true if the error is a ErrNotFound. As it uses a +// feature-test, it is also compatible with other NotFound error types, +// including github.com/ipld/go-ipld-prime/storage#ErrNotFound. func IsNotFound(err error) bool { - return errors.Is(err, ErrNotFound{}) + if nf, ok := err.(interface{ NotFound() bool }); ok { + return nf.NotFound() + } + return false } // Either a node or an error.