From 2551c10101351c77afe5db44c5dd9d7f54092002 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 +++++++++---- version.json | 2 +- 2 files changed, 10 insertions(+), 5 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. diff --git a/version.json b/version.json index 372b6ea..fc15ae0 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "version": "v0.4.0" + "version": "v0.5.0" }