Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Make ErrNotFound a struct and include IsNotFound helper #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (d *testDag) Get(ctx context.Context, cid *cid.Cid) (Node, error) {
if n, ok := d.nodes[cid.KeyString()]; ok {
return n, nil
}
return nil, ErrNotFound
return nil, ErrNotFound{cid}
}

func (d *testDag) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *NodeOption {
Expand All @@ -35,7 +35,7 @@ func (d *testDag) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *NodeOpti
if n, ok := d.nodes[c.KeyString()]; ok {
out <- &NodeOption{Node: n}
} else {
out <- &NodeOption{Err: ErrNotFound}
out <- &NodeOption{Err: ErrNotFound{c}}
}
}
close(out)
Expand Down
2 changes: 1 addition & 1 deletion daghelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetNodes(ctx context.Context, ds NodeGetter, keys []*cid.Cid) []*NodePromis
case opt, ok := <-nodechan:
if !ok {
for _, p := range promises {
p.Fail(ErrNotFound)
p.Fail(ErrNotFound{})
}
return
}
Expand Down
26 changes: 26 additions & 0 deletions errnotfound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package format

import (
"fmt"

cid "github.com/ipfs/go-cid"
errs "github.com/pkg/errors"
)

// ErrNotFound indicates that a CID was not found
type ErrNotFound struct {
Cid *cid.Cid
}

func (e ErrNotFound) Error() string {
if e.Cid == nil {
return fmt.Sprintf("CID not found")
}
return fmt.Sprintf("CID not found: %s", e.Cid)
}

// IsNotFound returns true if the cause of the error is ErrNotFound
func IsNotFound(e error) bool {
_, ok := errs.Cause(e).(ErrNotFound)
return ok
}
23 changes: 23 additions & 0 deletions errnotfound_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package format

import (
"fmt"
"testing"

errs "github.com/pkg/errors"
)

func TestIsNotFound(t *testing.T) {
err1 := ErrNotFound{}
if !IsNotFound(err1) {
t.Errorf("IsNotFound not true when it should be")
}
err2 := errs.Wrap(err1, "wrapped")
if !IsNotFound(err2) {
t.Errorf("IsNotFound not true on wrapped error when it should be")
}
err3 := fmt.Errorf("CID not found")
if IsNotFound(err3) {
t.Errorf("IsNotFound true when it should not be")
}
}
3 changes: 0 additions & 3 deletions merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package format

import (
"context"
"fmt"

cid "github.com/ipfs/go-cid"
)

var ErrNotFound = fmt.Errorf("merkledag: not found")

// Either a node or an error.
type NodeOption struct {
Node Node
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"hash": "QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua",
"name": "go-multihash",
"version": "1.0.7"
},
{
"author": "whyrusleeping",
"hash": "QmVmDhyTTUcQXFD1rRQ64fGLMSAoaQvNH3hwuaCFAPq2hy",
"name": "errors",
"version": "0.0.1"
}
],
"gxVersion": "0.10.0",
Expand Down