Skip to content

Commit

Permalink
Merge pull request #55 from ipfs/feat/bettererror2
Browse files Browse the repository at this point in the history
Improve "merkledag: not found" error
  • Loading branch information
hsanjuan authored Mar 26, 2020
2 parents 2d6a45d + 3d8b689 commit 2c2e0d1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
language: go

go:
- 1.12.x
- 1.14.x

env:
global:
Expand Down
25 changes: 23 additions & 2 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package format

import (
"context"
"errors"
"fmt"
"sync"
"testing"

Expand All @@ -24,7 +26,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 +37,7 @@ func (d *testDag) GetMany(ctx context.Context, cids []cid.Cid) <-chan *NodeOptio
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 Expand Up @@ -144,3 +146,22 @@ func TestBatchOptions(t *testing.T) {
t.Fatalf("maxNodes incorrect, want: %d, got: %d", wantMaxNodes, b.opts.maxNodes)
}
}

func TestErrorTypes(t *testing.T) {
d := newTestDag()
notFoundNode := &EmptyNode{}
_, err := d.Get(context.Background(), notFoundNode.Cid())
if err == nil {
t.Fatal("should throw NotFound error")
}

err2 := fmt.Errorf("could not read: %w", err)

if !errors.Is(err, ErrNotFound{}) {
t.Fatal("should be an ErrNotFound")
}

if !errors.Is(err2, ErrNotFound{}) {
t.Fatal("should be an ErrNotFound")
}
}
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) []*NodePromise
case opt, ok := <-nodechan:
if !ok {
for _, p := range promises {
p.Fail(ErrNotFound)
p.Fail(ErrNotFound{})
}
return
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/ipfs/go-ipld-format

go 1.14

require (
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-cid v0.0.2
Expand Down
41 changes: 40 additions & 1 deletion merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,51 @@ package format

import (
"context"
"errors"
"fmt"

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

var ErrNotFound = fmt.Errorf("merkledag: not found")
// ErrNotFound is used to signal when a Node could not be found. The specific
// meaning will depend on the DAGService implementation, which may be trying
// to read nodes locally but also, trying to find them remotely.
//
// The Cid field can be filled in to provide additional context.
type ErrNotFound struct {
Cid cid.Cid
}

// Error implements the error interface and returns a human-readable
// message for this error.
func (e ErrNotFound) Error() string {
if e.Cid == cid.Undef {
return "ipld: node not found"
}

return fmt.Sprintf("ipld: %s not found", e.Cid)
}

// Is allows to check whether any error is of this ErrNotFound type.
// Do not use this directly, but rather errors.Is(yourError, ErrNotFound).
func (e ErrNotFound) Is(err error) bool {
switch err.(type) {
case ErrNotFound:
return true
default:
return false
}
}

func (e ErrNotFound) NotFound() bool {
return true
}

// IsNotFound returns if the given error is or wraps an ErrNotFound
// (equivalent to errors.Is(err, ErrNotFound{}))
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound{})
}

// Either a node or an error.
type NodeOption struct {
Expand Down

0 comments on commit 2c2e0d1

Please sign in to comment.