Skip to content

Commit

Permalink
feat!: use NotFound feature-test in IsNotFound()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rvagg committed Feb 16, 2023
1 parent e379dec commit 2551c10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package format

import (
"context"
"errors"

cid "github.com/ipfs/go-cid"
)
Expand All @@ -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:
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.4.0"
"version": "v0.5.0"
}

0 comments on commit 2551c10

Please sign in to comment.