7
7
)
8
8
9
9
// 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).
12
13
//
13
14
// ErrNotFound implements `interface{NotFound() bool}`, which makes it roughly
14
15
// compatible with the legacy github.com/ipfs/go-ipld-format#ErrNotFound.
@@ -23,20 +24,30 @@ import (
23
24
// matching function that should be able to determine whether an ErrNotFound,
24
25
// either new or legacy, exists within a wrapped error chain.
25
26
type ErrNotFound struct {
26
- Key string
27
+ Cid cid. Cid
27
28
}
28
29
29
30
// NewErrNotFound is a convenience factory that creates a new ErrNotFound error
30
31
// from a CID.
31
32
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 }
33
44
}
34
45
35
46
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"
38
49
}
39
- return "ipld: could not find " + e .Key
50
+ return "ipld: could not find " + e .Cid . String ()
40
51
}
41
52
42
53
// NotFound always returns true, and is used to feature-test for ErrNotFound
0 commit comments