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

Commit 825a5ec

Browse files
committed
feat: add blockstore: block not found matching too
1 parent 794adc8 commit 825a5ec

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (api *BlockAPI) Get(ctx context.Context, p path.Path) (io.Reader, error) {
6666
return nil, err
6767
}
6868
if resp.Error != nil {
69-
return nil, parseIPLDNotFoundWithFallbackToError(resp.Error)
69+
return nil, parseErrNotFoundWithFallbackToError(resp.Error)
7070
}
7171

7272
//TODO: make get return ReadCloser to avoid copying
@@ -98,14 +98,14 @@ func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRm
9898
return err
9999
}
100100

101-
return parseIPLDNotFoundWithFallbackToMSG(removedBlock.Error)
101+
return parseErrNotFoundWithFallbackToMSG(removedBlock.Error)
102102
}
103103

104104
func (api *BlockAPI) Stat(ctx context.Context, p path.Path) (iface.BlockStat, error) {
105105
var out blockStat
106106
err := api.core().Request("block/stat", p.String()).Exec(ctx, &out)
107107
if err != nil {
108-
return nil, parseIPLDNotFoundWithFallbackToError(err)
108+
return nil, parseErrNotFoundWithFallbackToError(err)
109109
}
110110
out.cid, err = cid.Parse(out.Key)
111111
if err != nil {

errors.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
ipld "github.com/ipfs/go-ipld-format"
99
)
1010

11+
// This file handle parsing and returning the correct ABI based errors from error messages
12+
1113
type prePostWrappedNotFoundError struct {
1214
pre string
1315
post string
@@ -27,17 +29,17 @@ func (e prePostWrappedNotFoundError) Unwrap() error {
2729
return e.wrapped
2830
}
2931

30-
func parseIPLDNotFoundWithFallbackToMSG(msg string) error {
31-
err, handled := parseIPLDNotFound(msg)
32+
func parseErrNotFoundWithFallbackToMSG(msg string) error {
33+
err, handled := parseErrNotFound(msg)
3234
if handled {
3335
return err
3436
}
3537

3638
return errors.New(msg)
3739
}
3840

39-
func parseIPLDNotFoundWithFallbackToError(msg error) error {
40-
err, handled := parseIPLDNotFound(msg.Error())
41+
func parseErrNotFoundWithFallbackToError(msg error) error {
42+
err, handled := parseErrNotFound(msg.Error())
4143
if handled {
4244
return err
4345
}
@@ -57,13 +59,25 @@ func notAsciiLetterOrDigits(r rune) bool {
5759
return notAsciiLetterOrDigitsLUT[r] > 0
5860
}
5961

60-
// This file handle parsing and returning the correct ABI based errors from error messages
6162
//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) {
6364
if msg == "" {
6465
return nil, true // Fast path
6566
}
6667

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) {
6781
// The patern we search for is:
6882
const ipldErrNotFoundKey = "ipld: could not find " /*CID*/
6983
// 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) {
114128

115129
return err, true
116130
}
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+
}

errors_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var randomSha256MH = mh.Multihash{0x12, 0x20, 0x88, 0x82, 0x73, 0x37, 0x7c, 0xc1
1616
func doParseIpldNotFoundTest(t *testing.T, original error) {
1717
originalMsg := original.Error()
1818

19-
rebuilt := parseIPLDNotFoundWithFallbackToMSG(originalMsg)
19+
rebuilt := parseErrNotFoundWithFallbackToMSG(originalMsg)
2020

2121
rebuiltMsg := rebuilt.Error()
2222

@@ -32,7 +32,7 @@ func doParseIpldNotFoundTest(t *testing.T, original error) {
3232
}
3333

3434
func TestParseIPLDNotFound(t *testing.T) {
35-
if err := parseIPLDNotFoundWithFallbackToMSG(""); err != nil {
35+
if err := parseErrNotFoundWithFallbackToMSG(""); err != nil {
3636
t.Errorf("expected empty string to give no error; got %T %q", err, err.Error())
3737
}
3838

@@ -59,6 +59,27 @@ func TestParseIPLDNotFound(t *testing.T) {
5959
}
6060
}
6161

62+
func TestBlockstoreNotFoundMatchingIPLDErrNotFound(t *testing.T) {
63+
if !ipld.IsNotFound(blockstoreNotFoundMatchingIPLDErrNotFound{}) {
64+
t.Fatalf("expected blockstoreNotFoundMatchingIPLDErrNotFound to match ipld.IsNotFound; got false")
65+
}
66+
67+
for _, wrap := range [...]string{
68+
"",
69+
"merkledag: %w",
70+
"testing: %w the test",
71+
"%w is wrong",
72+
} {
73+
var err error = blockstoreNotFoundMatchingIPLDErrNotFound{"blockstore: block not found"}
74+
75+
if wrap != "" {
76+
err = fmt.Errorf(wrap, err)
77+
}
78+
79+
doParseIpldNotFoundTest(t, err)
80+
}
81+
}
82+
6283
func TestNotAsciiLetterOrDigits(t *testing.T) {
6384
for i := rune(0); i <= 256; i++ {
6485
if notAsciiLetterOrDigits(i) != !strings.ContainsAny(string(i), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {

0 commit comments

Comments
 (0)