diff --git a/cmd/booster-bitswap/remoteblockstore/remoteblockstore.go b/cmd/booster-bitswap/remoteblockstore/remoteblockstore.go index efc2eaa60..35b83ef64 100644 --- a/cmd/booster-bitswap/remoteblockstore/remoteblockstore.go +++ b/cmd/booster-bitswap/remoteblockstore/remoteblockstore.go @@ -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 } diff --git a/cmd/booster-bitswap/remoteblockstore/remoteblockstore_test.go b/cmd/booster-bitswap/remoteblockstore/remoteblockstore_test.go index 1bbd32974..1c80585d7 100644 --- a/cmd/booster-bitswap/remoteblockstore/remoteblockstore_test.go +++ b/cmd/booster-bitswap/remoteblockstore/remoteblockstore_test.go @@ -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"), @@ -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 { @@ -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()) + } }) } }