8
8
ipld "github.com/ipfs/go-ipld-format"
9
9
)
10
10
11
+ // This file handle parsing and returning the correct ABI based errors from error messages
12
+
11
13
type prePostWrappedNotFoundError struct {
12
14
pre string
13
15
post string
@@ -27,17 +29,17 @@ func (e prePostWrappedNotFoundError) Unwrap() error {
27
29
return e .wrapped
28
30
}
29
31
30
- func parseIPLDNotFoundWithFallbackToMSG (msg string ) error {
31
- err , handled := parseIPLDNotFound (msg )
32
+ func parseErrNotFoundWithFallbackToMSG (msg string ) error {
33
+ err , handled := parseErrNotFound (msg )
32
34
if handled {
33
35
return err
34
36
}
35
37
36
38
return errors .New (msg )
37
39
}
38
40
39
- func parseIPLDNotFoundWithFallbackToError (msg error ) error {
40
- err , handled := parseIPLDNotFound (msg .Error ())
41
+ func parseErrNotFoundWithFallbackToError (msg error ) error {
42
+ err , handled := parseErrNotFound (msg .Error ())
41
43
if handled {
42
44
return err
43
45
}
@@ -57,13 +59,25 @@ func notAsciiLetterOrDigits(r rune) bool {
57
59
return notAsciiLetterOrDigitsLUT [r ] > 0
58
60
}
59
61
60
- // This file handle parsing and returning the correct ABI based errors from error messages
61
62
//lint:ignore ST1008 this function is not using the error as a mean to return failure but it massages it to return the correct type
62
- func parseIPLDNotFound (msg string ) (error , bool ) {
63
+ func parseErrNotFound (msg string ) (error , bool ) {
63
64
if msg == "" {
64
65
return nil , true // Fast path
65
66
}
66
67
68
+ if err , handled := parseIPLDErrNotFound (msg ); handled {
69
+ return err , true
70
+ }
71
+
72
+ if err , handled := parseBlockstoreNotFound (msg ); handled {
73
+ return err , true
74
+ }
75
+
76
+ return nil , false
77
+ }
78
+
79
+ //lint:ignore ST1008 using error as values
80
+ func parseIPLDErrNotFound (msg string ) (error , bool ) {
67
81
// The patern we search for is:
68
82
const ipldErrNotFoundKey = "ipld: could not find " /*CID*/
69
83
// We try to parse the CID, if it's invalid we give up and return a simple text error.
@@ -114,3 +128,33 @@ func parseIPLDNotFound(msg string) (error, bool) {
114
128
115
129
return err , true
116
130
}
131
+
132
+ // This is a simple error type that just return msg as Error().
133
+ // But that also match ipld.ErrNotFound when called with Is(err).
134
+ // That is needed to keep compatiblity with code that use string.Contains(err.Error(), "blockstore: block not found")
135
+ // and code using ipld.ErrNotFound
136
+ type blockstoreNotFoundMatchingIPLDErrNotFound struct {
137
+ msg string
138
+ }
139
+
140
+ func (e blockstoreNotFoundMatchingIPLDErrNotFound ) String () string {
141
+ return e .Error ()
142
+ }
143
+
144
+ func (e blockstoreNotFoundMatchingIPLDErrNotFound ) Error () string {
145
+ return e .msg
146
+ }
147
+
148
+ func (e blockstoreNotFoundMatchingIPLDErrNotFound ) Is (err error ) bool {
149
+ _ , ok := err .(ipld.ErrNotFound )
150
+ return ok
151
+ }
152
+
153
+ //lint:ignore ST1008 using error as values
154
+ func parseBlockstoreNotFound (msg string ) (error , bool ) {
155
+ if ! strings .Contains (msg , "blockstore: block not found" ) {
156
+ return nil , false
157
+ }
158
+
159
+ return blockstoreNotFoundMatchingIPLDErrNotFound {msg : msg }, true
160
+ }
0 commit comments