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

Commit 794adc8

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

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ require (
2121
)
2222

2323
go 1.16
24+
25+
replace github.com/ipfs/go-ipld-format => github.com/Jorropo/go-ipld-format v0.3.2-0.20220330014726-942265d1aca7
26+
27+
replace github.com/ipfs/interface-go-ipfs-core => github.com/Jorropo/interface-go-ipfs-core v0.6.2-0.20220331215619-b98f8571cf6b

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOv
1111
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
1212
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
1313
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
14+
github.com/Jorropo/go-ipld-format v0.3.2-0.20220330014726-942265d1aca7 h1:G1yNzr09VZCoOjXsrl6jQk5HF/Ju+eGtYGrimZMu9DY=
15+
github.com/Jorropo/go-ipld-format v0.3.2-0.20220330014726-942265d1aca7/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
16+
github.com/Jorropo/interface-go-ipfs-core v0.6.2-0.20220331215619-b98f8571cf6b h1:uS/dAAwEsTGDj70E3Z01n5U7xje3jtsSAV4wuuH+5xg=
17+
github.com/Jorropo/interface-go-ipfs-core v0.6.2-0.20220331215619-b98f8571cf6b/go.mod h1:h3NuO3wzv2KuKazt0zDF2/i8AFRqiKHusyh5DUQQdPA=
1418
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
1519
github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y=
1620
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
@@ -337,12 +341,6 @@ github.com/ipfs/go-ipld-cbor v0.0.2/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA
337341
github.com/ipfs/go-ipld-cbor v0.0.3/go.mod h1:wTBtrQZA3SoFKMVkp6cn6HMRteIB1VsmHA0AQFOn7Nc=
338342
github.com/ipfs/go-ipld-cbor v0.0.5 h1:ovz4CHKogtG2KB/h1zUp5U0c/IzZrL435rCh5+K/5G8=
339343
github.com/ipfs/go-ipld-cbor v0.0.5/go.mod h1:BkCduEx3XBCO6t2Sfo5BaHzuok7hbhdMm9Oh8B2Ftq4=
340-
github.com/ipfs/go-ipld-format v0.0.1/go.mod h1:kyJtbkDALmFHv3QR6et67i35QzO3S0dCDnkOJhcZkms=
341-
github.com/ipfs/go-ipld-format v0.0.2/go.mod h1:4B6+FM2u9OJ9zCV+kSbgFAZlOrv1Hqbf0INGQgiKf9k=
342-
github.com/ipfs/go-ipld-format v0.2.0/go.mod h1:3l3C1uKoadTPbeNfrDi+xMInYKlx2Cvg1BuydPSdzQs=
343-
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=
346344
github.com/ipfs/go-ipld-legacy v0.1.0 h1:wxkkc4k8cnvIGIjPO0waJCe7SHEyFgl+yQdafdjGrpA=
347345
github.com/ipfs/go-ipld-legacy v0.1.0/go.mod h1:86f5P/srAmh9GcIcWQR9lfFLZPrIyyXQeVlOWeeWEuI=
348346
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
@@ -378,8 +376,6 @@ github.com/ipfs/go-unixfs v0.2.5/go.mod h1:SUdisfUjNoSDzzhGVxvCL9QO/nKdwXdr+gbMU
378376
github.com/ipfs/go-unixfsnode v1.1.2/go.mod h1:5dcE2x03pyjHk4JjamXmunTMzz+VUtqvPwZjIEkfV6s=
379377
github.com/ipfs/go-verifcid v0.0.1 h1:m2HI7zIuR5TFyQ1b79Da5N9dnnCP1vcu2QqawmWlK2E=
380378
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=
383379
github.com/ipfs/iptb v1.4.0 h1:YFYTrCkLMRwk/35IMyC6+yjoQSHTEcNcefBStLJzgvo=
384380
github.com/ipfs/iptb v1.4.0/go.mod h1:1rzHpCYtNp87/+hTxG5TfCVn/yMY3dKnLn8tBiMfdmg=
385381
github.com/ipfs/iptb-plugins v0.3.0 h1:C1rpq1o5lUZtaAOkLIox5akh6ba4uk/3RwWc6ttVxw0=

0 commit comments

Comments
 (0)