Skip to content

use errrors.New instead of empty fmt.Errorf #557

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

Merged
merged 1 commit into from
Jun 14, 2024
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
10 changes: 5 additions & 5 deletions XDCxlending/lendingstate/lendingcontract.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lendingstate

import (
"fmt"
"errors"
"math/big"

"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
Expand Down Expand Up @@ -273,10 +273,10 @@ func GetAllLendingBooks(statedb *state.StateDB) (mapLendingBook map[common.Hash]
baseTokens := GetSupportedBaseToken(statedb)
terms := GetSupportedTerms(statedb)
if len(baseTokens) == 0 {
return nil, fmt.Errorf("GetAllLendingBooks: empty baseToken list")
return nil, errors.New("GetAllLendingBooks: empty baseToken list")
}
if len(terms) == 0 {
return nil, fmt.Errorf("GetAllLendingPairs: empty term list")
return nil, errors.New("GetAllLendingPairs: empty term list")
}
for _, baseToken := range baseTokens {
for _, term := range terms {
Expand All @@ -295,10 +295,10 @@ func GetAllLendingPairs(statedb *state.StateDB) (allPairs []LendingPair, err err
baseTokens := GetSupportedBaseToken(statedb)
collaterals := GetAllCollateral(statedb)
if len(baseTokens) == 0 {
return allPairs, fmt.Errorf("GetAllLendingPairs: empty baseToken list")
return allPairs, errors.New("GetAllLendingPairs: empty baseToken list")
}
if len(collaterals) == 0 {
return allPairs, fmt.Errorf("GetAllLendingPairs: empty collateral list")
return allPairs, errors.New("GetAllLendingPairs: empty collateral list")
}
for _, baseToken := range baseTokens {
for _, collateral := range collaterals {
Expand Down
7 changes: 4 additions & 3 deletions XDCxlending/lendingstate/lendingitem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lendingstate

import (
"errors"
"fmt"
"math/big"
"strconv"
Expand Down Expand Up @@ -359,7 +360,7 @@ func (l *LendingItem) VerifyLendingSignature() error {
tx.ImportSignature(V, R, S)
from, _ := types.LendingSender(types.LendingTxSigner{}, tx)
if from != tx.UserAddress() {
return fmt.Errorf("verify lending item: invalid signature")
return errors.New("verify lending item: invalid signature")
}
return nil
}
Expand Down Expand Up @@ -473,10 +474,10 @@ func VerifyBalance(isXDCXLendingFork bool, statedb *state.StateDB, lendingStateD
}
return nil
default:
return fmt.Errorf("VerifyBalance: unknown lending side")
return errors.New("VerifyBalance: unknown lending side")
}
default:
return fmt.Errorf("VerifyBalance: unknown lending type")
return errors.New("VerifyBalance: unknown lending type")
}
return nil
}
7 changes: 4 additions & 3 deletions XDCxlending/order_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package XDCxlending

import (
"encoding/json"
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -264,7 +265,7 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address
borrowFee = lendingstate.GetFee(statedb, oldestOrder.Relayer)
}
if collateralToken.IsZero() {
return nil, nil, nil, fmt.Errorf("empty collateral")
return nil, nil, nil, errors.New("empty collateral")
}
depositRate, liquidationRate, recallRate := lendingstate.GetCollateralDetail(statedb, collateralToken)
if depositRate == nil || depositRate.Sign() <= 0 {
Expand All @@ -282,10 +283,10 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address
return nil, nil, nil, err
}
if lendTokenXDCPrice == nil || lendTokenXDCPrice.Sign() <= 0 {
return nil, nil, nil, fmt.Errorf("invalid lendToken price")
return nil, nil, nil, errors.New("invalid lendToken price")
}
if collateralPrice == nil || collateralPrice.Sign() <= 0 {
return nil, nil, nil, fmt.Errorf("invalid collateral price")
return nil, nil, nil, errors.New("invalid collateral price")
}
tradedQuantity, collateralLockedAmount, rejectMaker, settleBalanceResult, err := l.getLendQuantity(lendTokenXDCPrice, collateralPrice, depositRate, borrowFee, coinbase, chain, header, statedb, order, &oldestOrder, maxTradedQuantity)
if err != nil && err == lendingstate.ErrQuantityTradeTooSmall && tradedQuantity != nil && tradedQuantity.Sign() >= 0 {
Expand Down
6 changes: 3 additions & 3 deletions accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
// Unpack output in v according to the abi specification
func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
if len(output) == 0 {
return fmt.Errorf("abi: unmarshalling empty output")
return errors.New("abi: unmarshalling empty output")
}
// since there can't be naming collisions with contracts and events,
// we need to decide whether we're calling a method or an event
if method, ok := abi.Methods[name]; ok {
if len(output)%32 != 0 {
return fmt.Errorf("abi: improperly formatted output")
return errors.New("abi: improperly formatted output")
}
return method.Outputs.Unpack(v, output)
} else if event, ok := abi.Events[name]; ok {
return event.Inputs.Unpack(v, output)
}
return fmt.Errorf("abi: could not locate named method or event")
return errors.New("abi: could not locate named method or event")
}

// UnmarshalJSON implements json.Unmarshaler interface
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
return errNoEventSignature
}
if log.Topics[0] != c.abi.Events[event].Id() {
return fmt.Errorf("event signature mismatch")
return errors.New("event signature mismatch")
}
if len(log.Data) > 0 {
if err := c.abi.Unpack(out, event, log.Data); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions accounts/abi/bind/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package bind

import (
"context"
"fmt"
"errors"
"time"

"github.com/XinFinOrg/XDPoSChain/common"
Expand Down Expand Up @@ -56,14 +56,14 @@ func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*ty
// contract address when it is mined. It stops waiting when ctx is canceled.
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
if tx.To() != nil {
return common.Address{}, fmt.Errorf("tx is not contract creation")
return common.Address{}, errors.New("tx is not contract creation")
}
receipt, err := WaitMined(ctx, b, tx)
if err != nil {
return common.Address{}, err
}
if receipt.ContractAddress == (common.Address{}) {
return common.Address{}, fmt.Errorf("zero address")
return common.Address{}, errors.New("zero address")
}
// Check that code has indeed been deployed at the address.
// This matters on pre-Homestead chains: OOG in the constructor
Expand Down
3 changes: 2 additions & 1 deletion accounts/abi/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package abi

import (
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -117,7 +118,7 @@ func requireUniqueStructFieldNames(args Arguments) error {
for _, arg := range args {
field := capitalise(arg.Name)
if field == "" {
return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
return errors.New("abi: purely underscored output cannot unpack to struct")
}
if exists[field] {
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)
Expand Down
5 changes: 3 additions & 2 deletions accounts/abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package abi

import (
"errors"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -61,7 +62,7 @@ var (
func NewType(t string) (typ Type, err error) {
// check that array brackets are equal if they exist
if strings.Count(t, "[") != strings.Count(t, "]") {
return Type{}, fmt.Errorf("invalid arg type in abi")
return Type{}, errors.New("invalid arg type in abi")
}

typ.stringKind = t
Expand Down Expand Up @@ -98,7 +99,7 @@ func NewType(t string) (typ Type, err error) {
}
typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type)
} else {
return Type{}, fmt.Errorf("invalid formatting of array type")
return Type{}, errors.New("invalid formatting of array type")
}
return typ, err
}
Expand Down
7 changes: 4 additions & 3 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package abi

import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"reflect"
Expand Down Expand Up @@ -70,7 +71,7 @@ func readBool(word []byte) (bool, error) {
// This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
if t.T != FunctionTy {
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array")
}
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
Expand All @@ -83,7 +84,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
// through reflection, creates a fixed array to be read from
func readFixedBytes(t Type, word []byte) (interface{}, error) {
if t.T != FixedBytesTy {
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
return nil, errors.New("abi: invalid type in call to make fixed byte array")
}
// convert
array := reflect.New(t.Type).Elem()
Expand Down Expand Up @@ -123,7 +124,7 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
// declare our array
refSlice = reflect.New(t.Type).Elem()
} else {
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
}

// Arrays have packed elements, resulting in longer unpack steps.
Expand Down
3 changes: 2 additions & 1 deletion accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package keystore

import (
"errors"
"fmt"
"math/rand"
"os"
Expand Down Expand Up @@ -305,7 +306,7 @@ func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
select {
case <-ks.changes:
default:
return fmt.Errorf("wasn't notified of new accounts")
return errors.New("wasn't notified of new accounts")
}
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"crypto/ecdsa"
crand "crypto/rand"
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -455,7 +454,7 @@ func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (ac
func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (accounts.Account, error) {
key := newKeyFromECDSA(priv)
if ks.cache.hasAddress(key.Address) {
return accounts.Account{}, fmt.Errorf("account already exists")
return accounts.Account{}, errors.New("account already exists")
}
return ks.importKey(key, passphrase)
}
Expand Down
5 changes: 3 additions & 2 deletions bmt/bmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package bmt
import (
"bytes"
crand "crypto/rand"
"errors"
"fmt"
"hash"
"io"
Expand Down Expand Up @@ -288,7 +289,7 @@ func TestHasherConcurrency(t *testing.T) {
var err error
select {
case <-time.NewTimer(5 * time.Second).C:
err = fmt.Errorf("timed out")
err = errors.New("timed out")
case err = <-errc:
}
if err != nil {
Expand Down Expand Up @@ -321,7 +322,7 @@ func testHasherCorrectness(bmt hash.Hash, hasher BaseHasher, d []byte, n, count
}()
select {
case <-timeout.C:
err = fmt.Errorf("BMT hash calculation timed out")
err = errors.New("BMT hash calculation timed out")
case err = <-c:
}
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils

import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -130,7 +131,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
for batch := 0; ; batch++ {
// Load a batch of RLP blocks.
if checkInterrupt() {
return fmt.Errorf("interrupted")
return errors.New("interrupted")
}
i := 0
for ; i < importBatchSize; i++ {
Expand All @@ -153,7 +154,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
}
// Import the batch.
if checkInterrupt() {
return fmt.Errorf("interrupted")
return errors.New("interrupted")
}
missing := missingBlocks(chain, blocks[:i])
if len(missing) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions common/countdown/countdown_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package countdown

import (
"fmt"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -76,7 +76,7 @@ func TestCountdownShouldResetEvenIfErrored(t *testing.T) {
called := make(chan int)
OnTimeoutFn := func(time.Time, interface{}) error {
called <- 1
return fmt.Errorf("ERROR!")
return errors.New("ERROR!")
}

countdown := NewCountDown(5000 * time.Millisecond)
Expand Down
3 changes: 2 additions & 1 deletion consensus/XDPoS/XDPoS.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package XDPoS

import (
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -438,7 +439,7 @@ func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *type
case params.ConsensusEngineVersion2:
return x.EngineV2.CalculateMissingRounds(chain, header)
default: // Default "v1"
return nil, fmt.Errorf("Not supported in the v1 consensus")
return nil, errors.New("Not supported in the v1 consensus")
}
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/XDPoS/engines/engine_v1/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainRea
if no%epoch == 0 {
cpHeader = header
} else {
return common.Address{}, fmt.Errorf("couldn't find checkpoint header")
return common.Address{}, errors.New("couldn't find checkpoint header")
}
}
m, err := getM1M2FromCheckpointHeader(cpHeader, header, chain.Config())
Expand Down
Loading