diff --git a/storage/memstore/memstore.go b/storage/memstore/memstore.go index b484e834..7a0369e0 100644 --- a/storage/memstore/memstore.go +++ b/storage/memstore/memstore.go @@ -57,7 +57,7 @@ func (store *Store) Get(ctx context.Context, key string) ([]byte, error) { store.beInitialized() content, exists := store.Bag[key] if !exists { - return nil, storage.ErrNotFound{Key: key} + return nil, storage.NewErrNotFoundForKey(key) } cpy := make([]byte, len(content)) copy(cpy, content) @@ -83,7 +83,7 @@ func (store *Store) Put(ctx context.Context, key string, content []byte) error { func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, error) { content, exists := store.Bag[key] if !exists { - return nil, storage.ErrNotFound{Key: key} + return nil, storage.NewErrNotFoundForKey(key) } return noopCloser{bytes.NewReader(content)}, nil } @@ -92,7 +92,7 @@ func (store *Store) GetStream(ctx context.Context, key string) (io.ReadCloser, e func (store *Store) Peek(ctx context.Context, key string) ([]byte, io.Closer, error) { content, exists := store.Bag[key] if !exists { - return nil, nil, storage.ErrNotFound{Key: key} + return nil, nil, storage.NewErrNotFoundForKey(key) } return content, noopCloser{nil}, nil } diff --git a/storage/notfound.go b/storage/notfound.go index 0795a5a6..8496d856 100644 --- a/storage/notfound.go +++ b/storage/notfound.go @@ -7,8 +7,9 @@ import ( ) // ErrNotFound is a 404, but for block storage systems. It is returned when -// a block is not found. The Key is typically the binary form of a CID -// (CID#KeyString()). +// a block is not found. The Cid property may be cid.Undef if the NotFound error +// was not created with a specific CID (e.g. when using a non-CID key in a +// storage Get operation). // // ErrNotFound implements `interface{NotFound() bool}`, which makes it roughly // compatible with the legacy github.com/ipfs/go-ipld-format#ErrNotFound. @@ -23,20 +24,30 @@ import ( // matching function that should be able to determine whether an ErrNotFound, // either new or legacy, exists within a wrapped error chain. type ErrNotFound struct { - Key string + Cid cid.Cid } // NewErrNotFound is a convenience factory that creates a new ErrNotFound error // from a CID. func NewErrNotFound(c cid.Cid) ErrNotFound { - return ErrNotFound{Key: c.KeyString()} + return ErrNotFound{Cid: c} +} + +// NewErrNotFound is a convenience factory that creates a new ErrNotFound error +// from a key. If the key is a CID#KeyString(), then it will be cast to a CID, +// otherwise the Cid of the ErrNotFound will be cid.Undef. +func NewErrNotFoundForKey(key string) ErrNotFound { + if c, err := cid.Cast([]byte(key)); err == nil { + return ErrNotFound{Cid: c} + } + return ErrNotFound{Cid: cid.Undef} } func (e ErrNotFound) Error() string { - if c, err := cid.Cast([]byte(e.Key)); err == nil && c != cid.Undef { - return "ipld: could not find " + c.String() + if e.Cid == cid.Undef { + return "ipld: could not find node" } - return "ipld: could not find " + e.Key + return "ipld: could not find " + e.Cid.String() } // NotFound always returns true, and is used to feature-test for ErrNotFound diff --git a/storage/notfound_test.go b/storage/notfound_test.go index 56d207d2..f8a223a5 100644 --- a/storage/notfound_test.go +++ b/storage/notfound_test.go @@ -10,14 +10,14 @@ import ( ) func TestNotFound(t *testing.T) { - nf := storage.ErrNotFound{Key: "foo"} + nf := storage.ErrNotFound{} if !storage.IsNotFound(nf) { t.Fatal("expected ErrNotFound to be a NotFound error") } if !errors.Is(nf, storage.ErrNotFound{}) { t.Fatal("expected ErrNotFound to be a NotFound error") } - if nf.Error() != "ipld: could not find foo" { + if nf.Error() != "ipld: could not find node" { t.Fatal("unexpected error message") }