Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit 296534f

Browse files
committed
feat: update the error parsing for go-ipld-format to v0.4.0
1 parent 2e09c4b commit 296534f

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

errors.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ func parseIPLDNotFoundWithFallbackToError(msg error) error {
4545
return msg
4646
}
4747

48+
// Use a string to move it into RODATA
49+
// print("".join("\\x01" if chr(i) not in string.ascii_letters + string.digits else "\\x00" for i in range(ord('z')+1)))
50+
const notAsciiLetterOrDigitsLUT = "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
51+
52+
func notAsciiLetterOrDigits(r rune) bool {
53+
if r > 'z' {
54+
return true
55+
}
56+
57+
return notAsciiLetterOrDigitsLUT[r] > 0
58+
}
59+
4860
// This file handle parsing and returning the correct ABI based errors from error messages
4961
//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
5062
func parseIPLDNotFound(msg string) (error, bool) {
@@ -53,46 +65,46 @@ func parseIPLDNotFound(msg string) (error, bool) {
5365
}
5466

5567
// The patern we search for is:
56-
// node not found (fallback)
57-
// or
58-
// CID not found (here we parse the CID)
59-
notFoundIndex := strings.LastIndex(msg, " not found")
68+
const ipldErrNotFoundKey = "ipld: could not find " /*CID*/
69+
// We try to parse the CID, if it's invalid we give up and return a simple text error.
70+
// We also accept "node" in place of the CID because that means it's an Undefined CID.
6071

61-
if notFoundIndex == -1 {
62-
// Unknown, ot found not found
72+
keyIndex := strings.Index(msg, ipldErrNotFoundKey)
73+
74+
if keyIndex < 0 { // Unknown error
6375
return nil, false
6476
}
6577

66-
preNotFound := msg[:notFoundIndex]
78+
cidStart := keyIndex + len(ipldErrNotFoundKey)
6779

80+
msgPostKey := msg[cidStart:]
6881
var c cid.Cid
69-
var preIndex int
70-
if strings.HasSuffix(preNotFound, "node") {
82+
var postIndex int
83+
if strings.HasPrefix(msgPostKey, "node") {
7184
// Fallback case
7285
c = cid.Undef
73-
preIndex = notFoundIndex - len("node")
86+
postIndex = len("node")
7487
} else {
75-
// Assume that CIDs does not include whitespace to pull out the CID
76-
preIndex = strings.LastIndexByte(preNotFound, ' ')
77-
// + 1 is to normalise not founds to zeros and point to the start of the CID, not the previous space
78-
preIndex++
88+
// Assume that CIDs only contain a-zA-Z0-9 characters.
89+
// This is true because go-ipld-format use go-cid#Cid.String which use base{3{2,6},58}.
90+
postIndex = strings.IndexFunc(msgPostKey, notAsciiLetterOrDigits)
91+
if postIndex < 0 {
92+
postIndex = len(msgPostKey)
93+
}
94+
7995
var err error
80-
c, err = cid.Decode(preNotFound[preIndex:])
96+
c, err = cid.Decode(msgPostKey[:postIndex])
8197
if err != nil {
8298
// Unknown
8399
return nil, false
84100
}
85101
}
86102

87-
postIndex := notFoundIndex + len(" not found")
88-
89103
err := ipld.ErrNotFound{Cid: c}
90-
91-
pre := msg[:preIndex]
92-
post := msg[postIndex:]
104+
pre := msg[:keyIndex]
105+
post := msgPostKey[postIndex:]
93106

94107
if len(pre) > 0 || len(post) > 0 {
95-
// We have some text to wrap arround the ErrNotFound one
96108
return prePostWrappedNotFoundError{
97109
pre: pre,
98110
post: post,

errors_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httpapi
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67
"testing"
78

89
"github.com/ipfs/go-cid"
@@ -42,9 +43,8 @@ func TestParseIPLDNotFound(t *testing.T) {
4243
"%w is wrong",
4344
} {
4445
for _, err := range [...]error{
45-
errors.New("file not found"),
46-
errors.New(" not found"),
47-
errors.New("Bad_CID not found"),
46+
errors.New("ipld: could not find "),
47+
errors.New("ipld: could not find Bad_CID"),
4848
errors.New("network connection timeout"),
4949
ipld.ErrNotFound{Cid: cid.Undef},
5050
ipld.ErrNotFound{Cid: cid.NewCidV0(randomSha256MH)},
@@ -58,3 +58,11 @@ func TestParseIPLDNotFound(t *testing.T) {
5858
}
5959
}
6060
}
61+
62+
func TestNotAsciiLetterOrDigits(t *testing.T) {
63+
for i := rune(0); i <= 256; i++ {
64+
if notAsciiLetterOrDigits(i) != !strings.ContainsAny(string(i), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
65+
t.Errorf("%q is incorrectly identified", i)
66+
}
67+
}
68+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ require (
55
github.com/ipfs/go-cid v0.0.7
66
github.com/ipfs/go-ipfs-cmds v0.6.0
77
github.com/ipfs/go-ipfs-files v0.0.8
8-
github.com/ipfs/go-ipld-format v0.3.1
8+
github.com/ipfs/go-ipld-format v0.4.0
99
github.com/ipfs/go-merkledag v0.6.0
1010
github.com/ipfs/go-path v0.1.1
1111
github.com/ipfs/go-unixfs v0.2.5
12-
github.com/ipfs/interface-go-ipfs-core v0.6.1
12+
github.com/ipfs/interface-go-ipfs-core v0.6.2
1313
github.com/ipfs/iptb v1.4.0
1414
github.com/ipfs/iptb-plugins v0.3.0
1515
github.com/libp2p/go-libp2p-core v0.8.6

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dC
341341
github.com/ipfs/go-ipld-format v0.0.2/go.mod h1:4B6+FM2u9OJ9zCV+kSbgFAZlOrv1Hqbf0INGQgiKf9k=
342342
github.com/ipfs/go-ipld-format v0.2.0/go.mod h1:3l3C1uKoadTPbeNfrDi+xMInYKlx2Cvg1BuydPSdzQs=
343343
github.com/ipfs/go-ipld-format v0.3.0/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
344-
github.com/ipfs/go-ipld-format v0.3.1 h1:Ps7yQWHdM/QUZhk08/KzYsXw4z+w/r8CIyR8XL/x6Hk=
345-
github.com/ipfs/go-ipld-format v0.3.1/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
344+
github.com/ipfs/go-ipld-format v0.4.0 h1:yqJSaJftjmjc9jEOFYlpkwOLVKv68OD27jFLlSghBlQ=
345+
github.com/ipfs/go-ipld-format v0.4.0/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
346346
github.com/ipfs/go-ipld-legacy v0.1.0 h1:wxkkc4k8cnvIGIjPO0waJCe7SHEyFgl+yQdafdjGrpA=
347347
github.com/ipfs/go-ipld-legacy v0.1.0/go.mod h1:86f5P/srAmh9GcIcWQR9lfFLZPrIyyXQeVlOWeeWEuI=
348348
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
@@ -378,8 +378,8 @@ github.com/ipfs/go-unixfs v0.2.5/go.mod h1:SUdisfUjNoSDzzhGVxvCL9QO/nKdwXdr+gbMU
378378
github.com/ipfs/go-unixfsnode v1.1.2/go.mod h1:5dcE2x03pyjHk4JjamXmunTMzz+VUtqvPwZjIEkfV6s=
379379
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
380380
github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0=
381-
github.com/ipfs/interface-go-ipfs-core v0.6.1 h1:V0bV1PWUtKVfrisi1qbeY9wwex+apRu2ZufTc9Tvqkg=
382-
github.com/ipfs/interface-go-ipfs-core v0.6.1/go.mod h1:h3NuO3wzv2KuKazt0zDF2/i8AFRqiKHusyh5DUQQdPA=
381+
github.com/ipfs/interface-go-ipfs-core v0.6.2 h1:nnkq9zhb5O8lPzkZeynEymc83RqkTRqfYH4x5JNUkT4=
382+
github.com/ipfs/interface-go-ipfs-core v0.6.2/go.mod h1:h3NuO3wzv2KuKazt0zDF2/i8AFRqiKHusyh5DUQQdPA=
383383
github.com/ipfs/iptb v1.4.0 h1:YFYTrCkLMRwk/35IMyC6+yjoQSHTEcNcefBStLJzgvo=
384384
github.com/ipfs/iptb v1.4.0/go.mod h1:1rzHpCYtNp87/+hTxG5TfCVn/yMY3dKnLn8tBiMfdmg=
385385
github.com/ipfs/iptb-plugins v0.3.0 h1:C1rpq1o5lUZtaAOkLIox5akh6ba4uk/3RwWc6ttVxw0=

0 commit comments

Comments
 (0)