Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate all not found errors to ipld ErrNotFound for bitswap #869

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
})
}
}