Skip to content

Commit 3142e13

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 db67fbd commit 3142e13

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

storage/benchmarks/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5 // indirect
1717
github.com/gogo/protobuf v1.3.2 // indirect
1818
github.com/google/uuid v1.3.0 // indirect
19-
github.com/ipfs/go-cid v0.4.0 // indirect
19+
github.com/ipfs/go-cid v0.4.1 // indirect
2020
github.com/ipfs/go-datastore v0.6.0 // indirect
2121
github.com/ipfs/go-log v1.0.3 // indirect
2222
github.com/ipfs/go-log/v2 v2.0.3 // indirect
@@ -27,7 +27,7 @@ require (
2727
github.com/multiformats/go-base32 v0.0.3 // indirect
2828
github.com/multiformats/go-base36 v0.1.0 // indirect
2929
github.com/multiformats/go-multibase v0.0.3 // indirect
30-
github.com/multiformats/go-multihash v0.2.1 // indirect
30+
github.com/multiformats/go-multihash v0.2.2 // indirect
3131
github.com/multiformats/go-varint v0.0.6 // indirect
3232
github.com/opentracing/opentracing-go v1.1.0 // indirect
3333
github.com/spaolacci/murmur3 v1.1.0 // indirect

storage/benchmarks/go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
1212
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1313
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
1414
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15-
github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA=
16-
github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
15+
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
16+
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
1717
github.com/ipfs/go-datastore v0.5.0/go.mod h1:9zhEApYMTl17C8YDp7JmU7sQZi2/wqiYh73hakZ90Bk=
1818
github.com/ipfs/go-datastore v0.6.0 h1:JKyz+Gvz1QEZw0LsX1IBn+JFCJQH4SJVFtM4uWU0Myk=
1919
github.com/ipfs/go-datastore v0.6.0/go.mod h1:rt5M3nNbSO/8q1t4LNkLyUwRs8HupMeN/8O4Vn9YAT8=
@@ -52,8 +52,8 @@ github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ8
5252
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
5353
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
5454
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
55-
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108=
56-
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc=
55+
github.com/multiformats/go-multihash v0.2.2 h1:Uu7LWs/PmWby1gkj1S1DXx3zyd3aVabA4FiMKn/2tAc=
56+
github.com/multiformats/go-multihash v0.2.2/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM=
5757
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=
5858
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
5959
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=

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)