feat: convert all error assetion into errors.Is

This commit is contained in:
danidomi 2024-08-16 20:15:34 +01:00
parent bd57f35f8d
commit 0ca33d4399
31 changed files with 74 additions and 55 deletions

View file

@ -87,7 +87,7 @@ func TestWaitDeployed(t *testing.T) {
select {
case <-mined:
if err != test.wantErr {
if !errors.Is(err, test.wantErr) {
t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err)
}
if address != test.wantAddress {

View file

@ -1106,7 +1106,7 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
{Type: ty},
}
decoded, err := decodeABI.Unpack(packed)
if err != testCase.err {
if !errors.Is(err, testCase.err) {
t.Fatalf("Expected error %v, actual error %v. case %d", testCase.err, err, i)
}
if err != nil {

View file

@ -17,6 +17,7 @@
package keystore
import (
"errors"
"math/rand"
"os"
"runtime"
@ -127,7 +128,7 @@ func TestTimedUnlock(t *testing.T) {
// Signing without passphrase fails because account is locked
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData)
if err != ErrLocked {
if !errors.Is(err, ErrLocked) {
t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err)
}
@ -145,7 +146,7 @@ func TestTimedUnlock(t *testing.T) {
// Signing fails again after automatic locking
time.Sleep(250 * time.Millisecond)
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData)
if err != ErrLocked {
if !errors.Is(err, ErrLocked) {
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
}
}
@ -185,7 +186,7 @@ func TestOverrideUnlock(t *testing.T) {
// Signing fails again after automatic locking
time.Sleep(250 * time.Millisecond)
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData)
if err != ErrLocked {
if !errors.Is(err, ErrLocked) {
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
}
}

View file

@ -17,15 +17,17 @@
package main
import (
"errors"
"fmt"
"os"
"github.com/urfave/cli/v2"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"
)
var (
@ -237,7 +239,7 @@ func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []str
log.Info("Unlocked account", "address", account.Address.Hex())
return ambiguousAddrRecovery(ks, err, password), password
}
if err != keystore.ErrDecrypt {
if !errors.Is(err, keystore.ErrDecrypt) {
// No need to prompt again if the error is not decryption-related.
break
}

View file

@ -22,6 +22,7 @@ import (
"bytes"
"container/list"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
@ -106,7 +107,7 @@ func rlpToText(in *inStream, out io.Writer) error {
stream := rlp.NewStream(in, 0)
for {
if err := dump(in, stream, 0, out); err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
return err
}
break

View file

@ -18,6 +18,7 @@ package bitutil
import (
"bytes"
"errors"
"fmt"
"math/rand"
"testing"
@ -107,7 +108,7 @@ func TestDecodingCycle(t *testing.T) {
data := hexutil.MustDecode(tt.input)
orig, err := bitsetDecodeBytes(data, tt.size)
if err != tt.fail {
if !errors.Is(err, tt.fail) {
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
}
if err != nil {

View file

@ -1391,7 +1391,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
// Write downloaded chain data and corresponding receipt chain data
if len(ancientBlocks) > 0 {
if n, err := writeAncient(ancientBlocks, ancientReceipts); err != nil {
if err == errInsertionInterrupted {
if errors.Is(err, errInsertionInterrupted) {
return 0, nil
}
return n, err
@ -1399,7 +1399,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
}
if len(liveBlocks) > 0 {
if n, err := writeLive(liveBlocks, liveReceipts); err != nil {
if err == errInsertionInterrupted {
if errors.Is(err, errInsertionInterrupted) {
return 0, nil
}
return n, err

View file

@ -26,6 +26,8 @@ import (
"testing"
"time"
"github.com/holiman/uint256"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/consensus"
@ -40,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/holiman/uint256"
)
// So we can deterministically seed different blockchains
@ -154,7 +155,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
err = blockchain.validator.ValidateBody(block)
}
if err != nil {
if err == ErrKnownBlock {
if errors.Is(err, ErrKnownBlock) {
continue
}
return err

View file

@ -19,6 +19,7 @@ package rawdb
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"os"
@ -68,7 +69,7 @@ func TestFreezerBasics(t *testing.T) {
}
// Check that we cannot read too far
_, err = f.Retrieve(uint64(255))
if err != errOutOfBounds {
if !errors.Is(err, errOutOfBounds) {
t.Fatal(err)
}
}
@ -878,7 +879,7 @@ func checkRetrieveError(t *testing.T, f *freezerTable, items map[uint64]error) {
if err == nil {
t.Fatalf("unexpected value %x for item %d, want error %v", item, value, wantError)
}
if err != wantError {
if !errors.Is(err, wantError) {
t.Fatalf("wrong error for item %d: %v", item, err)
}
}

View file

@ -106,7 +106,7 @@ func TestFreezerModifyRollback(t *testing.T) {
require.NoError(t, op.AppendRaw("test", 2, make([]byte, 2048)))
return theError
})
if err != theError {
if !errors.Is(err, theError) {
t.Errorf("ModifyAncients returned wrong error %q", err)
}
checkAncientCount(t, f, "test", 0)
@ -374,7 +374,7 @@ func checkAncientCount(t *testing.T, f *Freezer, kind string, n uint64) {
}
if _, err := f.Ancient(kind, index); err == nil {
t.Errorf("Ancient(%q, %d) didn't return expected error", kind, index)
} else if err != errOutOfBounds {
} else if !errors.Is(err, errOutOfBounds) {
t.Errorf("Ancient(%q, %d) returned unexpected error %q", kind, index, err)
}
}

View file

@ -96,7 +96,7 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error {
// Parse the next transaction and terminate on error
tx := new(types.Transaction)
if err = stream.Decode(tx); err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
failure = err
}
if batch.Len() > 0 {

View file

@ -19,6 +19,7 @@ package types
import (
"bytes"
"encoding/json"
"errors"
"math"
"math/big"
"reflect"
@ -300,7 +301,7 @@ func TestDecodeEmptyTypedReceipt(t *testing.T) {
input := []byte{0x80}
var r Receipt
err := rlp.DecodeBytes(input, &r)
if err != errShortTypedReceipt {
if !errors.Is(err, errShortTypedReceipt) {
t.Fatal("wrong error:", err)
}
}

View file

@ -76,7 +76,7 @@ func TestDecodeEmptyTypedTx(t *testing.T) {
input := []byte{0x80}
var tx Transaction
err := rlp.DecodeBytes(input, &tx)
if err != errShortTypedTx {
if !errors.Is(err, errShortTypedTx) {
t.Fatal("wrong error:", err)
}
}
@ -571,7 +571,7 @@ func TestYParityJSONUnmarshalling(t *testing.T) {
// Unmarshal the tx
var tx Transaction
err = tx.UnmarshalJSON(jsonBytes)
if err != test.wantErr {
if !errors.Is(err, test.wantErr) {
t.Fatalf("wrong error: got %v, want %v", err, test.wantErr)
}
})

View file

@ -251,7 +251,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
// when we're in homestead this also counts for code storage gas errors.
if err != nil {
evm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
if !errors.Is(err, ErrExecutionReverted) {
if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil {
evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
}
@ -310,7 +310,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
}
if err != nil {
evm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
if !errors.Is(err, ErrExecutionReverted) {
if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil {
evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
}
@ -360,7 +360,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
}
if err != nil {
evm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
if !errors.Is(err, ErrExecutionReverted) {
if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil {
evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
}
@ -421,7 +421,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
}
if err != nil {
evm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
if !errors.Is(err, ErrExecutionReverted) {
if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil {
evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
}
@ -562,7 +562,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// when we're in homestead this also counts for code storage gas errors.
if err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) {
evm.StateDB.RevertToSnapshot(snapshot)
if err != ErrExecutionReverted {
if !errors.Is(err, ErrExecutionReverted) {
contract.UseGas(contract.Gas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution)
}
}

View file

@ -152,12 +152,12 @@ func TestTooBigSharedKey(t *testing.T) {
}
_, err = prv1.GenerateShared(&prv2.PublicKey, 32, 32)
if err != ErrSharedKeyTooBig {
if !errors.Is(err, ErrSharedKeyTooBig) {
t.Fatal("ecdh: shared key should be too large for curve")
}
_, err = prv2.GenerateShared(&prv1.PublicKey, 32, 32)
if err != ErrSharedKeyTooBig {
if !errors.Is(err, ErrSharedKeyTooBig) {
t.Fatal("ecdh: shared key should be too large for curve")
}
}
@ -355,7 +355,7 @@ func TestBasicKeyValidation(t *testing.T) {
for _, b := range badBytes {
ct[0] = b
_, err := prv.Decrypt(ct, nil, nil)
if err != ErrInvalidPublicKey {
if !errors.Is(err, ErrInvalidPublicKey) {
t.Fatal("ecies: validated an invalid key")
}
}

View file

@ -12,6 +12,7 @@ import (
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"errors"
"io"
"testing"
)
@ -91,7 +92,7 @@ func TestInvalidRecoveryID(t *testing.T) {
sig, _ := Sign(msg, seckey)
sig[64] = 99
_, err := RecoverPubkey(msg, sig)
if err != ErrInvalidRecoveryID {
if !errors.Is(err, ErrInvalidRecoveryID) {
t.Fatalf("got %q, want %q", err, ErrInvalidRecoveryID)
}
}

View file

@ -19,6 +19,7 @@ package filters
import (
"context"
"encoding/json"
"errors"
"math/big"
"strings"
"testing"
@ -382,7 +383,7 @@ func TestFilters(t *testing.T) {
if err == nil {
t.Fatal("expected error")
}
if err != context.DeadlineExceeded {
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got %v", err)
}
})

View file

@ -56,7 +56,7 @@ loop:
t.Fatalf("wrong int %d, want %d", got, want)
}
case err := <-sub.Err():
if err != errInts {
if !errors.Is(err, errInts) {
t.Fatalf("wrong error: got %q, want %q", err, errInts)
}
if want != 2 {

View file

@ -101,7 +101,7 @@ func TestNodeUsedDataDir(t *testing.T) {
// Create a second node based on the same data directory and ensure failure
_, err = New(&Config{DataDir: dir})
if err != ErrDatadirUsed {
if !errors.Is(err, ErrDatadirUsed) {
t.Fatalf("duplicate datadir failure mismatch: have %v, want %v", err, ErrDatadirUsed)
}
}

View file

@ -211,7 +211,7 @@ func TestUDPv4_responseTimeouts(t *testing.T) {
for i := 0; i < nReqs; i++ {
select {
case err := <-timeoutErr:
if err != errTimeout {
if !errors.Is(err, errTimeout) {
t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err)
}
nTimeoutsRecv++
@ -241,7 +241,7 @@ func TestUDPv4_findnodeTimeout(t *testing.T) {
toid := enode.ID{1, 2, 3, 4}
target := v4wire.Pubkey{4, 5, 6, 7}
result, err := test.udp.findnode(toid, toaddr, target)
if err != errTimeout {
if !errors.Is(err, errTimeout) {
t.Error("expected timeout error, got", err)
}
if len(result) > 0 {

View file

@ -26,6 +26,7 @@ import (
"time"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/crypto"
@ -95,7 +96,7 @@ func TestClientSyncTreeBadNode(t *testing.T) {
c := NewClient(Config{Resolver: r, Logger: testlog.Logger(t, log.LvlTrace)})
_, err := c.SyncTree("enrtree://AKPYQIUQIL7PSIACI32J7FGZW56E5FKHEFCCOFHILBIMW3M6LWXS2@n")
wantErr := nameError{name: "INDMVBZEEQ4ESVYAKGIYU74EAA.n", err: entryError{typ: "enr", err: errInvalidENR}}
if err != wantErr {
if !errors.Is(err, wantErr) {
t.Fatalf("expected sync error %q, got %q", wantErr, err)
}
}

View file

@ -17,10 +17,12 @@
package dnsdisc
import (
"errors"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p/enode"
)
@ -54,7 +56,7 @@ func TestParseRoot(t *testing.T) {
if !reflect.DeepEqual(e, test.e) {
t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
}
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
}
}
@ -131,7 +133,7 @@ func TestParseEntry(t *testing.T) {
if !reflect.DeepEqual(e, test.e) {
t.Errorf("test %d: wrong entry %s, want %s", i, spew.Sdump(e), spew.Sdump(test.e))
}
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("test %d: wrong error %q, want %q", i, err, test.err)
}
}

View file

@ -19,14 +19,16 @@ package enr
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"testing"
"time"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/rlp"
)
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
@ -274,7 +276,7 @@ func TestDecodeIncomplete(t *testing.T) {
for _, test := range tests {
var r Record
err := rlp.DecodeBytes(test.input, &r)
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("wrong error for %X: %v", test.input, err)
}
}

View file

@ -18,6 +18,7 @@ package p2p
import (
"bytes"
"errors"
"fmt"
"io"
"runtime"
@ -55,7 +56,7 @@ loop:
go func() {
if err := SendItems(rw1, 1); err == nil {
t.Error("EncodeMsg returned nil error")
} else if err != ErrPipeClosed {
} else if !errors.Is(err, ErrPipeClosed) {
t.Errorf("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
}
close(done)

View file

@ -17,6 +17,7 @@
package netutil
import (
"errors"
"fmt"
"math/rand"
"net"
@ -178,7 +179,7 @@ func TestCheckRelayIP(t *testing.T) {
for _, test := range tests {
err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr))
if err != test.want {
if !errors.Is(err, test.want) {
t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want)
}
}

View file

@ -138,7 +138,7 @@ func TestPeerProtoReadMsg(t *testing.T) {
select {
case err := <-errc:
if err != errProtocolReturned {
if !errors.Is(err, errProtocolReturned) {
t.Errorf("peer returned error: %v", err)
}
case <-time.After(2 * time.Second):

View file

@ -129,7 +129,7 @@ func TestSplitUint64(t *testing.T) {
if !bytes.Equal(rest, unhex(test.rest)) {
t.Errorf("test %d: rest mismatch: got %x, want %s (input %q)", i, rest, test.rest, test.input)
}
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
}
}
@ -213,7 +213,7 @@ func TestSplit(t *testing.T) {
if !bytes.Equal(rest, unhex(test.rest)) {
t.Errorf("test %d: rest mismatch: got %x, want %s", i, rest, test.rest)
}
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
}
}
@ -251,7 +251,7 @@ func TestReadSize(t *testing.T) {
for _, test := range tests {
size, err := readSize(unhex(test.input), test.slen)
if err != test.err {
if !errors.Is(err, test.err) {
t.Errorf("readSize(%s, %d): error mismatch: got %q, want %q", test.input, test.slen, err, test.err)
continue
}

View file

@ -174,7 +174,7 @@ func messageForReadError(err error) string {
} else {
return "read error"
}
} else if err != io.EOF {
} else if !errors.Is(err, io.EOF) {
return "parse error"
}
return ""

View file

@ -332,7 +332,7 @@ func (api *SignerAPI) startUSBListener() {
for _, wallet := range am.Wallets() {
if err := wallet.Open(""); err != nil {
log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
if err == usbwallet.ErrTrezorPINNeeded {
if errors.Is(err, usbwallet.ErrTrezorPINNeeded) {
go api.openTrezor(wallet.URL())
}
}
@ -348,7 +348,7 @@ func (api *SignerAPI) derivationLoop(events chan accounts.WalletEvent) {
case accounts.WalletArrived:
if err := event.Wallet.Open(""); err != nil {
log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err)
if err == usbwallet.ErrTrezorPINNeeded {
if errors.Is(err, usbwallet.ErrTrezorPINNeeded) {
go api.openTrezor(event.Wallet.URL())
}
}

View file

@ -19,6 +19,7 @@ package core_test
import (
"bytes"
"context"
"errors"
"fmt"
"math/big"
"os"
@ -155,7 +156,7 @@ func failCreateAccountWithPassword(ui *headlessUi, api *core.SignerAPI, password
func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
ui.approveCh <- "N"
addr, err := api.New(context.Background())
if err != core.ErrRequestDenied {
if !errors.Is(err, core.ErrRequestDenied) {
t.Fatal(err)
}
if addr != (common.Address{}) {
@ -212,7 +213,7 @@ func TestNewAcc(t *testing.T) {
if len(list) != 0 {
t.Fatalf("List should be empty")
}
if err != core.ErrRequestDenied {
if !errors.Is(err, core.ErrRequestDenied) {
t.Fatal("Expected deny")
}
}
@ -264,7 +265,7 @@ func TestSignTx(t *testing.T) {
if res != nil {
t.Errorf("Expected nil-response, got %v", res)
}
if err != keystore.ErrDecrypt {
if !errors.Is(err, keystore.ErrDecrypt) {
t.Errorf("Expected ErrLocked! %v", err)
}
control.approveCh <- "No way"
@ -272,7 +273,7 @@ func TestSignTx(t *testing.T) {
if res != nil {
t.Errorf("Expected nil-response, got %v", res)
}
if err != core.ErrRequestDenied {
if !errors.Is(err, core.ErrRequestDenied) {
t.Errorf("Expected ErrRequestDenied! %v", err)
}
// Sign with correct password

View file

@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
@ -201,7 +202,7 @@ func TestSignData(t *testing.T) {
if signature != nil {
t.Errorf("Expected nil-data, got %x", signature)
}
if err != keystore.ErrDecrypt {
if !errors.Is(err, keystore.ErrDecrypt) {
t.Errorf("Expected ErrLocked! '%v'", err)
}
control.approveCh <- "No way"
@ -209,7 +210,7 @@ func TestSignData(t *testing.T) {
if signature != nil {
t.Errorf("Expected nil-data, got %x", signature)
}
if err != core.ErrRequestDenied {
if !errors.Is(err, core.ErrRequestDenied) {
t.Errorf("Expected ErrRequestDenied! '%v'", err)
}
// text/plain