Skip to content

Commit 0b315e0

Browse files
authored
style: Use require.ErrorIs whenever possible (#1303)
1 parent e38b660 commit 0b315e0

30 files changed

+206
-262
lines changed

.avalanche-golangci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ linters:
104104
forbidigo:
105105
# Forbid the following identifiers (list of regexp).
106106
forbid:
107-
# - pattern: require\.Error$(# ErrorIs should be used instead)?
108-
# - pattern: require\.ErrorContains$(# ErrorIs should be used instead)?
109-
# - pattern: require\.EqualValues$(# Equal should be used instead)?
110-
# - pattern: require\.NotEqualValues$(# NotEqual should be used instead)?
107+
- pattern: require\.Error$(# ErrorIs should be used instead)?
108+
- pattern: require\.ErrorContains$(# ErrorIs should be used instead)?
109+
- pattern: require\.EqualValues$(# Equal should be used instead)?
110+
- pattern: require\.NotEqualValues$(# NotEqual should be used instead)?
111111
- pattern: ^(t|b|tb|f)\.(Fatal|Fatalf|Error|Errorf)$(# the require library should be used instead)?
112-
# - pattern: ^sort\.(Slice|Strings)$(# the slices package should be used instead)?
112+
- pattern: ^sort\.(Slice|Strings)$(# the slices package should be used instead)?
113113
# Exclude godoc examples from forbidigo checks.
114114
exclude-godoc-examples: false
115115
gosec:

accounts/abi/abi_extra_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestUnpackInputIntoInterface(t *testing.T) {
8181
err = abi.UnpackInputIntoInterface(&v, "receive", data, test.strictMode) // skips 4 byte selector
8282

8383
if test.expectedErrorSubstring != "" {
84-
require.ErrorContains(t, err, test.expectedErrorSubstring)
84+
require.ErrorContains(t, err, test.expectedErrorSubstring) //nolint:forbidigo // uses upstream code
8585
} else {
8686
require.NoError(t, err)
8787
require.Equal(t, input, v)

network/network.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535

3636
var (
3737
errAcquiringSemaphore = errors.New("error acquiring semaphore")
38+
errEmptyNodeID = errors.New("cannot send request to empty nodeID")
3839
errExpiredRequest = errors.New("expired request")
3940
errNoPeersFound = errors.New("no peers found matching version")
4041
_ Network = (*network)(nil)
@@ -187,7 +188,7 @@ func (n *network) SendAppRequestAny(ctx context.Context, minVersion *version.App
187188
// SendAppRequest sends request message bytes to specified nodeID, notifying the responseHandler on response or failure
188189
func (n *network) SendAppRequest(ctx context.Context, nodeID ids.NodeID, request []byte, responseHandler message.ResponseHandler) error {
189190
if nodeID == ids.EmptyNodeID {
190-
return fmt.Errorf("cannot send request to empty nodeID, nodeID=%s, requestLen=%d", nodeID, len(request))
191+
return fmt.Errorf("%w, nodeID=%s, requestLen=%d", errEmptyNodeID, nodeID, len(request))
191192
}
192193

193194
// If the context was cancelled, we can skip sending this request.

network/network_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,8 @@ func TestRequestRequestsRoutingAndResponse(t *testing.T) {
248248
}
249249

250250
// ensure empty nodeID is not allowed
251-
require.ErrorContains(t,
252-
net.SendAppRequest(context.Background(), ids.EmptyNodeID, []byte("hello there"), nil),
253-
"cannot send request to empty nodeID",
254-
)
251+
err = net.SendAppRequest(context.Background(), ids.EmptyNodeID, []byte("hello there"), nil)
252+
require.ErrorIs(t, err, errEmptyNodeID)
255253
}
256254

257255
func TestAppRequestOnShutdown(t *testing.T) {
@@ -554,7 +552,8 @@ func TestNetworkPropagatesRequestHandlerError(t *testing.T) {
554552
ctx := snowtest.Context(t, snowtest.CChainID)
555553
clientNetwork, err := NewNetwork(ctx, sender, codecManager, 1, prometheus.NewRegistry())
556554
require.NoError(t, err)
557-
clientNetwork.SetRequestHandler(&testRequestHandler{err: errors.New("fail")}) // Return an error from the request handler
555+
errTest := errors.New("test error")
556+
clientNetwork.SetRequestHandler(&testRequestHandler{err: errTest}) // Return an error from the request handler
558557

559558
require.NoError(t, clientNetwork.Connected(context.Background(), nodeID, defaultPeerVersion))
560559

@@ -565,7 +564,8 @@ func TestNetworkPropagatesRequestHandlerError(t *testing.T) {
565564
require.NoError(t, err)
566565

567566
// Check that if the request handler returns an error, it is propagated as a fatal error.
568-
require.ErrorContains(t, clientNetwork.AppRequest(context.Background(), nodeID, requestID, time.Now().Add(time.Second), requestMessage), "fail")
567+
err = clientNetwork.AppRequest(context.Background(), nodeID, requestID, time.Now().Add(time.Second), requestMessage)
568+
require.ErrorIs(t, err, errTest)
569569
}
570570

571571
func TestNetworkAppRequestAfterShutdown(t *testing.T) {

plugin/evm/atomic/export_tx.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ var (
3636
_ secp256k1fx.UnsignedTx = (*UnsignedExportTx)(nil)
3737
ErrExportNonAVAXInputBanff = errors.New("export input cannot contain non-AVAX in Banff")
3838
ErrExportNonAVAXOutputBanff = errors.New("export output cannot contain non-AVAX in Banff")
39+
ErrInsufficientFunds = errors.New("insufficient funds")
3940
ErrInvalidNonce = errors.New("invalid nonce")
4041
ErrNoExportOutputs = errors.New("tx has no export outputs")
4142
errOverflowExport = errors.New("overflow when computing export amount + txFee")
42-
errInsufficientFunds = errors.New("insufficient funds")
4343
)
4444

4545
// UnsignedExportTx is an unsigned ExportTx
@@ -323,14 +323,14 @@ func (utx *UnsignedExportTx) EVMStateTransfer(ctx *snow.Context, state StateDB)
323323
uint256.NewInt(X2CRate.Uint64()),
324324
)
325325
if state.GetBalance(from.Address).Cmp(amount) < 0 {
326-
return errInsufficientFunds
326+
return ErrInsufficientFunds
327327
}
328328
state.SubBalance(from.Address, amount)
329329
} else {
330330
log.Debug("export_tx", "dest", utx.DestinationChain, "addr", from.Address, "amount", from.Amount, "assetID", from.AssetID)
331331
amount := new(big.Int).SetUint64(from.Amount)
332332
if state.GetBalanceMultiCoin(from.Address, common.Hash(from.AssetID)).Cmp(amount) < 0 {
333-
return errInsufficientFunds
333+
return ErrInsufficientFunds
334334
}
335335
state.SubBalanceMultiCoin(from.Address, common.Hash(from.AssetID), amount)
336336
}
@@ -393,7 +393,7 @@ func getSpendableFunds(
393393
}
394394

395395
if amount > 0 {
396-
return nil, nil, errInsufficientFunds
396+
return nil, nil, ErrInsufficientFunds
397397
}
398398

399399
return inputs, signers, nil
@@ -487,7 +487,7 @@ func getSpendableAVAXWithFee(
487487
}
488488

489489
if amount > 0 {
490-
return nil, nil, errInsufficientFunds
490+
return nil, nil, ErrInsufficientFunds
491491
}
492492

493493
return inputs, signers, nil

plugin/evm/atomic/state/atomic_trie_iterator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ func TestIteratorHandlesInvalidData(t *testing.T) {
100100
require.NoError(err)
101101
for iter.Next() {
102102
}
103-
require.Error(iter.Error())
103+
err = iter.Error()
104+
require.ErrorIs(err, errKeyLength)
104105
}

plugin/evm/atomic/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
var (
3838
ErrWrongNetworkID = errors.New("tx was issued with a different network ID")
3939
ErrNilTx = errors.New("tx is nil")
40-
errNoValueOutput = errors.New("output has no value")
40+
ErrNoValueOutput = errors.New("output has no value")
4141
ErrNoValueInput = errors.New("input has no value")
4242
ErrNoGasUsed = errors.New("no gas used")
4343
errNilOutput = errors.New("nil output")
@@ -97,7 +97,7 @@ func (out *EVMOutput) Verify() error {
9797
case out == nil:
9898
return errNilOutput
9999
case out.Amount == 0:
100-
return errNoValueOutput
100+
return ErrNoValueOutput
101101
case out.AssetID == ids.Empty:
102102
return errEmptyAssetID
103103
}

0 commit comments

Comments
 (0)