Skip to content

Commit 64d7e0e

Browse files
committed
fix: change property of ErrNotFound - s/Key/Cid
ABI compatible with github.com/ipfs/go-ipld-format#ErrNotFound for smoother upgrade path
1 parent d2bcc84 commit 64d7e0e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

storage/memstore/memstore.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (store *Store) Get(ctx context.Context, key string) ([]byte, error) {
5757
store.beInitialized()
5858
content, exists := store.Bag[key]
5959
if !exists {
60-
return nil, storage.ErrNotFound{Key: key}
60+
return nil, storage.NewErrNotFoundForKey(key)
6161
}
6262
cpy := make([]byte, len(content))
6363
copy(cpy, content)
@@ -83,7 +83,7 @@ func (store *Store) Put(ctx context.Context, key string, content []byte) error {
8383
func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, error) {
8484
content, exists := store.Bag[key]
8585
if !exists {
86-
return nil, storage.ErrNotFound{Key: key}
86+
return nil, storage.NewErrNotFoundForKey(key)
8787
}
8888
return noopCloser{bytes.NewReader(content)}, nil
8989
}
@@ -92,7 +92,7 @@ func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, e
9292
func (store *Store) Peek(ctx context.Context, key string) ([]byte, io.Closer, error) {
9393
content, exists := store.Bag[key]
9494
if !exists {
95-
return nil, nil, storage.ErrNotFound{Key: key}
95+
return nil, nil, storage.NewErrNotFoundForKey(key)
9696
}
9797
return content, noopCloser{nil}, nil
9898
}

storage/notfound.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
// ErrNotFound is a 404, but for block storage systems. It is returned when
10-
// a block is not found. The Key is typically the binary form of a CID
11-
// (CID#KeyString()).
10+
// a block is not found. The Cid property may be cid.Undef if the NotFound error
11+
// was not created with a specific CID (e.g. when using a non-CID key in a
12+
// storage Get operation).
1213
//
1314
// ErrNotFound implements `interface{NotFound() bool}`, which makes it roughly
1415
// compatible with the legacy github.com/ipfs/go-ipld-format#ErrNotFound.
@@ -23,20 +24,30 @@ import (
2324
// matching function that should be able to determine whether an ErrNotFound,
2425
// either new or legacy, exists within a wrapped error chain.
2526
type ErrNotFound struct {
26-
Key string
27+
Cid cid.Cid
2728
}
2829

2930
// NewErrNotFound is a convenience factory that creates a new ErrNotFound error
3031
// from a CID.
3132
func NewErrNotFound(c cid.Cid) ErrNotFound {
32-
return ErrNotFound{Key: c.KeyString()}
33+
return ErrNotFound{Cid: c}
34+
}
35+
36+
// NewErrNotFound is a convenience factory that creates a new ErrNotFound error
37+
// from a key. If the key is a CID#KeyString(), then it will be cast to a CID,
38+
// otherwise the Cid of the ErrNotFound will be cid.Undef.
39+
func NewErrNotFoundForKey(key string) ErrNotFound {
40+
if c, err := cid.Cast([]byte(key)); err == nil {
41+
return ErrNotFound{Cid: c}
42+
}
43+
return ErrNotFound{Cid: cid.Undef}
3344
}
3445

3546
func (e ErrNotFound) Error() string {
36-
if c, err := cid.Cast([]byte(e.Key)); err == nil && c != cid.Undef {
37-
return "ipld: could not find " + c.String()
47+
if e.Cid == cid.Undef {
48+
return "ipld: could not find node"
3849
}
39-
return "ipld: could not find " + e.Key
50+
return "ipld: could not find " + e.Cid.String()
4051
}
4152

4253
// NotFound always returns true, and is used to feature-test for ErrNotFound

storage/notfound_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
)
1111

1212
func TestNotFound(t *testing.T) {
13-
nf := storage.ErrNotFound{Key: "foo"}
13+
nf := storage.ErrNotFound{}
1414
if !storage.IsNotFound(nf) {
1515
t.Fatal("expected ErrNotFound to be a NotFound error")
1616
}
1717
if !errors.Is(nf, storage.ErrNotFound{}) {
1818
t.Fatal("expected ErrNotFound to be a NotFound error")
1919
}
20-
if nf.Error() != "ipld: could not find foo" {
20+
if nf.Error() != "ipld: could not find node" {
2121
t.Fatal("unexpected error message")
2222
}
2323

0 commit comments

Comments
 (0)