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 cd97e05
Showing 1 changed file with 9 additions and 4 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

0 comments on commit cd97e05

Please sign in to comment.