Skip to content

Commit e9c3183

Browse files
authored
cmd: use errrors.New instead of empty fmt.Errorf (#27329)
Signed-off-by: jsvisa <delweng@gmail.com>
1 parent 9231770 commit e9c3183

20 files changed

+50
-38
lines changed

cmd/clef/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func initializeSecrets(c *cli.Context) error {
326326
return err
327327
}
328328
if num != len(masterSeed) {
329-
return fmt.Errorf("failed to read enough random")
329+
return errors.New("failed to read enough random")
330330
}
331331
n, p := keystore.StandardScryptN, keystore.StandardScryptP
332332
if c.Bool(utils.LightKDFFlag.Name) {
@@ -482,7 +482,7 @@ func initialize(c *cli.Context) error {
482482
}
483483
} else if !c.Bool(acceptFlag.Name) {
484484
if !confirm(legalWarning) {
485-
return fmt.Errorf("aborted by user")
485+
return errors.New("aborted by user")
486486
}
487487
fmt.Println()
488488
}
@@ -590,7 +590,7 @@ func accountImport(c *cli.Context) error {
590590
Address %v
591591
Keystore file: %v
592592
593-
The key is now encrypted; losing the password will result in permanently losing
593+
The key is now encrypted; losing the password will result in permanently losing
594594
access to the key and all associated funds!
595595
596596
Make sure to backup keystore and passwords in a safe location.`,
@@ -844,7 +844,7 @@ func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
844844
}
845845
masterSeed, err := decryptSeed(cipherKey, password)
846846
if err != nil {
847-
return nil, fmt.Errorf("failed to decrypt the master seed of clef")
847+
return nil, errors.New("failed to decrypt the master seed of clef")
848848
}
849849
if len(masterSeed) < 256 {
850850
return nil, fmt.Errorf("master seed of insufficient length, expected >255 bytes, got %d", len(masterSeed))

cmd/devp2p/discv4cmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"net"
2223
"strconv"
@@ -177,7 +178,7 @@ func discv4Resolve(ctx *cli.Context) error {
177178

178179
func discv4ResolveJSON(ctx *cli.Context) error {
179180
if ctx.NArg() < 1 {
180-
return fmt.Errorf("need nodes file as argument")
181+
return errors.New("need nodes file as argument")
181182
}
182183
nodesFile := ctx.Args().Get(0)
183184
inputSet := make(nodeSet)
@@ -207,7 +208,7 @@ func discv4ResolveJSON(ctx *cli.Context) error {
207208

208209
func discv4Crawl(ctx *cli.Context) error {
209210
if ctx.NArg() < 1 {
210-
return fmt.Errorf("need nodes file as argument")
211+
return errors.New("need nodes file as argument")
211212
}
212213
nodesFile := ctx.Args().First()
213214
var inputSet nodeSet

cmd/devp2p/discv5cmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"time"
2223

@@ -98,7 +99,7 @@ func discv5Resolve(ctx *cli.Context) error {
9899

99100
func discv5Crawl(ctx *cli.Context) error {
100101
if ctx.NArg() < 1 {
101-
return fmt.Errorf("need nodes file as argument")
102+
return errors.New("need nodes file as argument")
102103
}
103104
nodesFile := ctx.Args().First()
104105
var inputSet nodeSet

cmd/devp2p/dns_cloudflare.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"strings"
2324

@@ -48,7 +49,7 @@ type cloudflareClient struct {
4849
func newCloudflareClient(ctx *cli.Context) *cloudflareClient {
4950
token := ctx.String(cloudflareTokenFlag.Name)
5051
if token == "" {
51-
exit(fmt.Errorf("need cloudflare API token to proceed"))
52+
exit(errors.New("need cloudflare API token to proceed"))
5253
}
5354
api, err := cloudflare.NewWithAPIToken(token)
5455
if err != nil {

cmd/devp2p/dns_route53.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func newRoute53Client(ctx *cli.Context) *route53Client {
8181
akey := ctx.String(route53AccessKeyFlag.Name)
8282
asec := ctx.String(route53AccessSecretFlag.Name)
8383
if akey == "" || asec == "" {
84-
exit(fmt.Errorf("need Route53 Access Key ID and secret to proceed"))
84+
exit(errors.New("need Route53 Access Key ID and secret to proceed"))
8585
}
8686
creds := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(akey, asec, ""))
8787
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithCredentialsProvider(creds))

cmd/devp2p/dnscmd.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"crypto/ecdsa"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"os"
2425
"path/filepath"
@@ -147,7 +148,7 @@ func dnsSync(ctx *cli.Context) error {
147148

148149
func dnsSign(ctx *cli.Context) error {
149150
if ctx.NArg() < 2 {
150-
return fmt.Errorf("need tree definition directory and key file as arguments")
151+
return errors.New("need tree definition directory and key file as arguments")
151152
}
152153
var (
153154
defdir = ctx.Args().Get(0)
@@ -201,7 +202,7 @@ func directoryName(dir string) string {
201202
// dnsToTXT performs dnsTXTCommand.
202203
func dnsToTXT(ctx *cli.Context) error {
203204
if ctx.NArg() < 1 {
204-
return fmt.Errorf("need tree definition directory as argument")
205+
return errors.New("need tree definition directory as argument")
205206
}
206207
output := ctx.Args().Get(1)
207208
if output == "" {
@@ -218,7 +219,7 @@ func dnsToTXT(ctx *cli.Context) error {
218219
// dnsToCloudflare performs dnsCloudflareCommand.
219220
func dnsToCloudflare(ctx *cli.Context) error {
220221
if ctx.NArg() != 1 {
221-
return fmt.Errorf("need tree definition directory as argument")
222+
return errors.New("need tree definition directory as argument")
222223
}
223224
domain, t, err := loadTreeDefinitionForExport(ctx.Args().Get(0))
224225
if err != nil {
@@ -231,7 +232,7 @@ func dnsToCloudflare(ctx *cli.Context) error {
231232
// dnsToRoute53 performs dnsRoute53Command.
232233
func dnsToRoute53(ctx *cli.Context) error {
233234
if ctx.NArg() != 1 {
234-
return fmt.Errorf("need tree definition directory as argument")
235+
return errors.New("need tree definition directory as argument")
235236
}
236237
domain, t, err := loadTreeDefinitionForExport(ctx.Args().Get(0))
237238
if err != nil {
@@ -244,7 +245,7 @@ func dnsToRoute53(ctx *cli.Context) error {
244245
// dnsNukeRoute53 performs dnsRoute53NukeCommand.
245246
func dnsNukeRoute53(ctx *cli.Context) error {
246247
if ctx.NArg() != 1 {
247-
return fmt.Errorf("need domain name as argument")
248+
return errors.New("need domain name as argument")
248249
}
249250
client := newRoute53Client(ctx)
250251
return client.deleteDomain(ctx.Args().First())
@@ -363,10 +364,10 @@ func loadTreeDefinitionForExport(dir string) (domain string, t *dnsdisc.Tree, er
363364
// tree's signature if valid.
364365
func ensureValidTreeSignature(t *dnsdisc.Tree, pubkey *ecdsa.PublicKey, sig string) error {
365366
if sig == "" {
366-
return fmt.Errorf("missing signature, run 'devp2p dns sign' first")
367+
return errors.New("missing signature, run 'devp2p dns sign' first")
367368
}
368369
if err := t.SetSignature(pubkey, sig); err != nil {
369-
return fmt.Errorf("invalid signature on tree, run 'devp2p dns sign' to update it")
370+
return errors.New("invalid signature on tree, run 'devp2p dns sign' to update it")
370371
}
371372
return nil
372373
}

cmd/devp2p/enrcmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/base64"
2222
"encoding/hex"
23+
"errors"
2324
"fmt"
2425
"io"
2526
"net"
@@ -48,7 +49,7 @@ func enrdump(ctx *cli.Context) error {
4849
var source string
4950
if file := ctx.String(fileFlag.Name); file != "" {
5051
if ctx.NArg() != 0 {
51-
return fmt.Errorf("can't dump record from command-line argument in -file mode")
52+
return errors.New("can't dump record from command-line argument in -file mode")
5253
}
5354
var b []byte
5455
var err error
@@ -64,7 +65,7 @@ func enrdump(ctx *cli.Context) error {
6465
} else if ctx.NArg() == 1 {
6566
source = ctx.Args().First()
6667
} else {
67-
return fmt.Errorf("need record as argument")
68+
return errors.New("need record as argument")
6869
}
6970

7071
r, err := parseRecord(source)

cmd/devp2p/internal/ethtest/chain.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ethtest
1919
import (
2020
"compress/gzip"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"io"
2425
"math/big"
@@ -98,7 +99,7 @@ func (c *Chain) Head() *types.Block {
9899

99100
func (c *Chain) GetHeaders(req *GetBlockHeaders) ([]*types.Header, error) {
100101
if req.Amount < 1 {
101-
return nil, fmt.Errorf("no block headers requested")
102+
return nil, errors.New("no block headers requested")
102103
}
103104

104105
headers := make([]*types.Header, req.Amount)

cmd/devp2p/internal/ethtest/helpers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package ethtest
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"net"
2223
"reflect"
@@ -185,7 +186,7 @@ loop:
185186
}
186187
// make sure eth protocol version is set for negotiation
187188
if c.negotiatedProtoVersion == 0 {
188-
return nil, fmt.Errorf("eth protocol version must be set in Conn")
189+
return nil, errors.New("eth protocol version must be set in Conn")
189190
}
190191
if status == nil {
191192
// default status message

cmd/devp2p/internal/ethtest/transaction.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package ethtest
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/big"
2223
"strings"
@@ -39,7 +40,7 @@ func (s *Suite) sendSuccessfulTxs(t *utesting.T) error {
3940
}
4041
for i, tx := range tests {
4142
if tx == nil {
42-
return fmt.Errorf("could not find tx to send")
43+
return errors.New("could not find tx to send")
4344
}
4445
t.Logf("Testing tx propagation %d: sending tx %v %v %v\n", i, tx.Hash().String(), tx.GasPrice(), tx.Gas())
4546
// get previous tx if exists for reference in case of old tx propagation
@@ -348,7 +349,7 @@ func generateTxs(s *Suite, numTxs int) (map[common.Hash]common.Hash, []*types.Tr
348349

349350
nextTx := getNextTxFromChain(s)
350351
if nextTx == nil {
351-
return nil, nil, fmt.Errorf("failed to get the next transaction")
352+
return nil, nil, errors.New("failed to get the next transaction")
352353
}
353354
gas := nextTx.Gas()
354355

@@ -357,7 +358,7 @@ func generateTxs(s *Suite, numTxs int) (map[common.Hash]common.Hash, []*types.Tr
357358
for i := 0; i < numTxs; i++ {
358359
tx := generateTx(s.chain.chainConfig, nonce, gas)
359360
if tx == nil {
360-
return nil, nil, fmt.Errorf("failed to get the next transaction")
361+
return nil, nil, errors.New("failed to get the next transaction")
361362
}
362363
txHashMap[tx.Hash()] = tx.Hash()
363364
txs[i] = tx

cmd/devp2p/internal/ethtest/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package ethtest
1818

1919
import (
2020
"crypto/ecdsa"
21+
"errors"
2122
"fmt"
2223
"time"
2324

@@ -286,5 +287,5 @@ func (c *Conn) ReadSnap(id uint64) (Message, error) {
286287
}
287288
return snpMsg.(Message), nil
288289
}
289-
return nil, fmt.Errorf("request timed out")
290+
return nil, errors.New("request timed out")
290291
}

cmd/devp2p/internal/v4test/discv4tests.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v4test
1919
import (
2020
"bytes"
2121
"crypto/rand"
22+
"errors"
2223
"fmt"
2324
"net"
2425
"time"
@@ -119,7 +120,7 @@ func (te *testenv) checkPingPong(pingHash []byte) error {
119120
// and a PING. The two packets do not have to be in any particular order.
120121
func (te *testenv) checkPong(reply v4wire.Packet, pingHash []byte) error {
121122
if reply == nil {
122-
return fmt.Errorf("expected PONG reply, got nil")
123+
return errors.New("expected PONG reply, got nil")
123124
}
124125
if reply.Kind() != v4wire.PongPacket {
125126
return fmt.Errorf("expected PONG reply, got %v %v", reply.Name(), reply)

cmd/devp2p/keycmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"net"
2223

@@ -86,7 +87,7 @@ var (
8687

8788
func genkey(ctx *cli.Context) error {
8889
if ctx.NArg() != 1 {
89-
return fmt.Errorf("need key file as argument")
90+
return errors.New("need key file as argument")
9091
}
9192
file := ctx.Args().Get(0)
9293

@@ -126,7 +127,7 @@ func keyToRecord(ctx *cli.Context) error {
126127

127128
func makeRecord(ctx *cli.Context) (*enode.Node, error) {
128129
if ctx.NArg() != 1 {
129-
return nil, fmt.Errorf("need key file as argument")
130+
return nil, errors.New("need key file as argument")
130131
}
131132

132133
var (

cmd/devp2p/nodesetcmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var (
5959

6060
func nodesetInfo(ctx *cli.Context) error {
6161
if ctx.NArg() < 1 {
62-
return fmt.Errorf("need nodes file as argument")
62+
return errors.New("need nodes file as argument")
6363
}
6464

6565
ns := loadNodesJSON(ctx.Args().First())
@@ -98,7 +98,7 @@ func showAttributeCounts(ns nodeSet) {
9898

9999
func nodesetFilter(ctx *cli.Context) error {
100100
if ctx.NArg() < 1 {
101-
return fmt.Errorf("need nodes file as argument")
101+
return errors.New("need nodes file as argument")
102102
}
103103
// Parse -limit.
104104
limit, err := parseFilterLimit(ctx.Args().Tail())

cmd/devp2p/rlpxcmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"net"
2223

@@ -91,7 +92,7 @@ func rlpxPing(ctx *cli.Context) error {
9192
case 1:
9293
var msg []p2p.DiscReason
9394
if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
94-
return fmt.Errorf("invalid disconnect message")
95+
return errors.New("invalid disconnect message")
9596
}
9697
return fmt.Errorf("received disconnect message: %v", msg[0])
9798
default:

cmd/evm/internal/t8ntool/block.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,18 @@ func (i *bbInput) sealClique(block *types.Block) (*types.Block, error) {
171171
// If any clique value overwrites an explicit header value, fail
172172
// to avoid silently building a block with unexpected values.
173173
if i.Header.Extra != nil {
174-
return nil, NewError(ErrorConfig, fmt.Errorf("sealing with clique will overwrite provided extra data"))
174+
return nil, NewError(ErrorConfig, errors.New("sealing with clique will overwrite provided extra data"))
175175
}
176176
header := block.Header()
177177
if i.Clique.Voted != nil {
178178
if i.Header.Coinbase != nil {
179-
return nil, NewError(ErrorConfig, fmt.Errorf("sealing with clique and voting will overwrite provided coinbase"))
179+
return nil, NewError(ErrorConfig, errors.New("sealing with clique and voting will overwrite provided coinbase"))
180180
}
181181
header.Coinbase = *i.Clique.Voted
182182
}
183183
if i.Clique.Authorize != nil {
184184
if i.Header.Nonce != nil {
185-
return nil, NewError(ErrorConfig, fmt.Errorf("sealing with clique and voting will overwrite provided nonce"))
185+
return nil, NewError(ErrorConfig, errors.New("sealing with clique and voting will overwrite provided nonce"))
186186
}
187187
if *i.Clique.Authorize {
188188
header.Nonce = [8]byte{}

cmd/faucet/faucet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,6 @@ func getGenesis(genesisFlag string, goerliFlag bool, rinkebyFlag bool, sepoliaFl
893893
case sepoliaFlag:
894894
return core.DefaultSepoliaGenesisBlock(), nil
895895
default:
896-
return nil, fmt.Errorf("no genesis flag provided")
896+
return nil, errors.New("no genesis flag provided")
897897
}
898898
}

cmd/geth/chaincmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func dump(ctx *cli.Context) error {
478478
if conf.OnlyWithAddresses {
479479
fmt.Fprintf(os.Stderr, "If you want to include accounts with missing preimages, you need iterative output, since"+
480480
" otherwise the accounts will overwrite each other in the resulting mapping.")
481-
return fmt.Errorf("incompatible options")
481+
return errors.New("incompatible options")
482482
}
483483
fmt.Println(string(state.Dump(conf)))
484484
}

0 commit comments

Comments
 (0)