Skip to content

Commit

Permalink
fix: translate all not found errors to ipld ErrNotFound for bitswap (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored Oct 4, 2022
1 parent 39fd26d commit e33ed18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
28 changes: 17 additions & 11 deletions cmd/booster-bitswap/remoteblockstore/remoteblockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,25 @@ func normalizeError(err error) error {

// Check for ErrNotFound with a cid
idx = strings.Index(errMsg, ipldNotFoundPrefix)
if idx == -1 {
return err
}
if idx != -1 {
cidStr := errMsg[idx+len(ipldNotFoundPrefix):]
c, e := cid.Parse(cidStr)
if e != nil {
return err
}

cidStr := errMsg[idx+len(ipldNotFoundPrefix):]
c, e := cid.Parse(cidStr)
if e != nil {
return err
rest := errMsg[:idx]
if len(rest) > 2 && rest[len(rest)-2:] != ": " {
rest += ": "
}
return fmt.Errorf("%s%w", rest, format.ErrNotFound{Cid: c})
}

rest := errMsg[:idx]
if len(rest) > 2 && rest[len(rest)-2:] != ": " {
rest += ": "
// Check for any error with the string "not found"
idx = strings.Index(strings.ToLower(errMsg), "not found")
if idx != -1 {
return format.ErrNotFound{}
}
return fmt.Errorf("%s%w", rest, format.ErrNotFound{Cid: c})

return err
}
21 changes: 20 additions & 1 deletion cmd/booster-bitswap/remoteblockstore/remoteblockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func TestNormalizeError(t *testing.T) {
err: fmt.Errorf(fmt.Errorf("some err: %w", format.ErrNotFound{}).Error()),
expected: fmt.Errorf("some err: %w", format.ErrNotFound{}),
isNotFoundErr: true,
}, {
name: "block not found",
err: fmt.Errorf("block not found"),
expected: format.ErrNotFound{},
isNotFoundErr: true,
}, {
name: "different capitalization not found",
err: fmt.Errorf("block nOt FoUnd"),
expected: format.ErrNotFound{},
isNotFoundErr: true,
}, {
name: "different error",
err: fmt.Errorf("some other err"),
Expand All @@ -67,6 +77,11 @@ func TestNormalizeError(t *testing.T) {
err: fmt.Errorf(ipldNotFoundPrefix + "badcid"),
expected: fmt.Errorf(ipldNotFoundPrefix + "badcid"),
isNotFoundErr: false,
}, {
name: "nil error",
err: nil,
expected: nil,
isNotFoundErr: false,
}}

for _, tc := range testCases {
Expand All @@ -77,7 +92,11 @@ func TestNormalizeError(t *testing.T) {
} else {
require.False(t, format.IsNotFound(err))
}
require.Equal(t, tc.expected.Error(), err.Error())
if tc.err == nil {
require.Nil(t, err)
} else {
require.Equal(t, tc.expected.Error(), err.Error())
}
})
}
}

0 comments on commit e33ed18

Please sign in to comment.