Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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, ErrNotFoundCid{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: ErrNotFoundCid{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: 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
32 changes: 31 additions & 1 deletion merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,37 @@ import (
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.
var ErrNotFound = ErrNotFoundCid{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning ErrNotFoundCid{Cid} is already going to break anything comparing against ErrNotFound. Given that, I'd rather explicitly break it and replace ErrNotFound with type ErrNotFound struct { Cid }.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but no-one returns ErrNotFoundCid (and we can grep-fix all of our things accordingly). So for everyone else things would still work. But yeah, let's play safe and break.


// ErrNotFoundCid can be use to provide specific CID information in a NotFound
// error.
type ErrNotFoundCid struct {
c cid.Cid
}

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

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

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

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