From 6afcda712a000df3e2807b9333e1d855555ba179 Mon Sep 17 00:00:00 2001 From: LesCyber Date: Sun, 2 Mar 2025 22:15:39 +0800 Subject: [PATCH] refactor: use errors.New to replace fmt.Errorf with no parameters Signed-off-by: LesCyber --- amp/shard_tracker.go | 3 ++- amp/sharer.go | 4 ++-- cmd/commands/chainrpc_active.go | 5 ++--- cmd/commands/commands.go | 4 ++-- cmd/commands/peersrpc_active.go | 4 +--- cmd/commands/profile.go | 4 ++-- config.go | 6 +++--- config_builder.go | 6 +++--- fn/recv.go | 6 +++--- pilot.go | 2 +- rpcserver.go | 8 ++++---- server.go | 4 ++-- 12 files changed, 27 insertions(+), 29 deletions(-) diff --git a/amp/shard_tracker.go b/amp/shard_tracker.go index 473447e4e..477e14167 100644 --- a/amp/shard_tracker.go +++ b/amp/shard_tracker.go @@ -3,6 +3,7 @@ package amp import ( "crypto/rand" "encoding/binary" + "errors" "fmt" "sync" @@ -139,7 +140,7 @@ func (s *ShardTracker) CancelShard(pid uint64) error { c, ok := s.shards[pid] if !ok { - return fmt.Errorf("pid not found") + return errors.New("pid not found") } delete(s.shards, pid) diff --git a/amp/sharer.go b/amp/sharer.go index 661da994c..d35b9b201 100644 --- a/amp/sharer.go +++ b/amp/sharer.go @@ -2,7 +2,7 @@ package amp import ( "crypto/rand" - "fmt" + "errors" ) // zeroShare is the all-zero 32-byte share. @@ -97,7 +97,7 @@ func (s *SeedSharer) Root() Share { func (s *SeedSharer) Split() (Sharer, Sharer, error) { // We cannot split the zero-Sharer. if s.curr == zeroShare { - return nil, nil, fmt.Errorf("cannot split zero-Sharer") + return nil, nil, errors.New("cannot split zero-Sharer") } shareLeft, shareRight, err := split(&s.curr) diff --git a/cmd/commands/chainrpc_active.go b/cmd/commands/chainrpc_active.go index 0f1f8b612..e410792bb 100644 --- a/cmd/commands/chainrpc_active.go +++ b/cmd/commands/chainrpc_active.go @@ -5,7 +5,6 @@ package commands import ( "bytes" - "fmt" "strconv" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -80,7 +79,7 @@ func getBlock(ctx *cli.Context) error { blockHashString = args.First() default: - return fmt.Errorf("hash argument missing") + return errors.New("hash argument missing") } blockHash, err := chainhash.NewHashFromStr(blockHashString) @@ -233,7 +232,7 @@ func getBlockHash(ctx *cli.Context) error { } default: - return fmt.Errorf("block height argument missing") + return errors.New("block height argument missing") } client, cleanUp := getChainClient(ctx) diff --git a/cmd/commands/commands.go b/cmd/commands/commands.go index f41925392..a9b64aa32 100644 --- a/cmd/commands/commands.go +++ b/cmd/commands/commands.go @@ -267,7 +267,7 @@ func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error { c.Command.Name == "changepassword" || c.Command.Name == "createwatchonly") { - return fmt.Errorf("Wallet is already unlocked") + return errors.New("Wallet is already unlocked") } // lnd might be active, but not possible to contact @@ -277,7 +277,7 @@ func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error { // WalletUnlocker server active) and most likely this // is because of an encrypted wallet. if ok && s.Code() == codes.Unimplemented { - return fmt.Errorf("Wallet is encrypted. " + + return errors.New("Wallet is encrypted. " + "Please unlock using 'lncli unlock', " + "or set password using 'lncli create'" + " if this is the first time starting " + diff --git a/cmd/commands/peersrpc_active.go b/cmd/commands/peersrpc_active.go index 5a1fc85e3..23faf1237 100644 --- a/cmd/commands/peersrpc_active.go +++ b/cmd/commands/peersrpc_active.go @@ -4,8 +4,6 @@ package commands import ( - "fmt" - "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/peersrpc" "github.com/urfave/cli" @@ -147,7 +145,7 @@ func updateNodeAnnouncement(ctx *cli.Context) error { } if !change { - return fmt.Errorf("no changes for the node information " + + return errors.New("no changes for the node information " + "detected") } diff --git a/cmd/commands/profile.go b/cmd/commands/profile.go index 0d63ca93c..59aaf72b9 100644 --- a/cmd/commands/profile.go +++ b/cmd/commands/profile.go @@ -43,7 +43,7 @@ func (e *profileEntry) cert() (*x509.CertPool, error) { cp := x509.NewCertPool() if !cp.AppendCertsFromPEM([]byte(e.TLSCert)) { - return nil, fmt.Errorf("credentials: failed to append " + + return nil, errors.New("credentials: failed to append " + "certificate") } return cp, nil @@ -140,7 +140,7 @@ func profileFromContext(ctx *cli.Context, store, skipMacaroons bool) ( for _, m := range ctx.GlobalStringSlice("metadata") { pair := strings.Split(m, ":") if len(pair) != 2 { - return nil, fmt.Errorf("invalid format for metadata " + + return nil, errors.New("invalid format for metadata " + "flag; expected \"key:value\"") } diff --git a/config.go b/config.go index 8c3718e95..dcc9024a5 100644 --- a/config.go +++ b/config.go @@ -1930,7 +1930,7 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, splitCookie := strings.Split(string(cookie), ":") if len(splitCookie) != 2 { - return fmt.Errorf("cookie file has a wrong " + + return errors.New("cookie file has a wrong " + "format") } conf.RPCUser = splitCookie[0] @@ -1968,7 +1968,7 @@ func parseRPCParams(cConfig *lncfg.Chain, nodeConfig interface{}, // the RPC credentials from the configuration. So if lnd wasn't // specified the parameters, then we won't be able to start. if cConfig.SimNet { - return fmt.Errorf("rpcuser and rpcpass must be set to your " + + return errors.New("rpcuser and rpcpass must be set to your " + "btcd node's RPC parameters for simnet mode") } @@ -2097,7 +2097,7 @@ func extractBtcdRPCParams(btcdConfigPath string) (string, string, error) { } userSubmatches := rpcUserRegexp.FindSubmatch(configContents) if userSubmatches == nil { - return "", "", fmt.Errorf("unable to find rpcuser in config") + return "", "", errors.New("unable to find rpcuser in config") } // Similarly, we'll use another regular expression to find the set diff --git a/config_builder.go b/config_builder.go index 43c9e4a68..f8c54c93a 100644 --- a/config_builder.go +++ b/config_builder.go @@ -272,7 +272,7 @@ func (d *DefaultWalletImpl) ValidateMacaroon(ctx context.Context, // Because the default implementation does not return any permissions, // we shouldn't be registered as an external validator at all and this // should never be invoked. - return fmt.Errorf("default implementation does not support external " + + return errors.New("default implementation does not support external " + "macaroon validation") } @@ -376,7 +376,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context, if d.cfg.WalletUnlockPasswordFile != "" && !walletExists && !d.cfg.WalletUnlockAllowCreate { - return nil, nil, nil, fmt.Errorf("wallet unlock password file " + + return nil, nil, nil, errors.New("wallet unlock password file " + "was specified but wallet does not exist; initialize " + "the wallet before using auto unlocking") } @@ -1173,7 +1173,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( return nil, nil, err } if ripInvoices { - err = fmt.Errorf("invoices bucket tombstoned, please " + + err = errors.New("invoices bucket tombstoned, please " + "switch back to native SQL") d.logger.Error(err) diff --git a/fn/recv.go b/fn/recv.go index 254118076..9f4a45d8a 100644 --- a/fn/recv.go +++ b/fn/recv.go @@ -1,7 +1,7 @@ package fn import ( - "fmt" + "errors" "time" ) @@ -14,7 +14,7 @@ func RecvOrTimeout[T any](c <-chan T, timeout time.Duration) (T, error) { case <-time.After(timeout): var zero T - return zero, fmt.Errorf("timeout hit") + return zero, errors.New("timeout hit") } } @@ -33,6 +33,6 @@ func RecvResp[T any](r <-chan T, e <-chan error, q <-chan struct{}) (T, error) { return noResp, err case <-q: - return noResp, fmt.Errorf("quitting") + return noResp, errors.New("quitting") } } diff --git a/pilot.go b/pilot.go index 11333a072..3f6fa032e 100644 --- a/pilot.go +++ b/pilot.go @@ -66,7 +66,7 @@ func validateAtplCfg(cfg *lncfg.AutoPilot) ([]*autopilot.WeightedHeuristic, } if sum != 1.0 { - return nil, fmt.Errorf("heuristic weights must sum to 1.0") + return nil, errors.New("heuristic weights must sum to 1.0") } return heuristics, nil } diff --git a/rpcserver.go b/rpcserver.go index 7236a5dad..3291b7d38 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -226,7 +226,7 @@ var ( // If the --no-macaroons flag is used to start lnd, the macaroon service // is not initialized. errMacaroonDisabled is then returned when // macaroon related services are used. - errMacaroonDisabled = fmt.Errorf("macaroon authentication disabled, " + + errMacaroonDisabled = errors.New("macaroon authentication disabled, " + "remove --no-macaroons flag to enable") ) @@ -1355,7 +1355,7 @@ func (r *rpcServer) SendCoins(ctx context.Context, decodedAddr, _ := hex.DecodeString(in.Addr) _, err = btcec.ParsePubKey(decodedAddr) if err == nil { - return nil, fmt.Errorf("cannot send coins to pubkeys") + return nil, errors.New("cannot send coins to pubkeys") } label, err := labels.ValidateAPI(in.Label) @@ -1385,7 +1385,7 @@ func (r *rpcServer) SendCoins(ctx context.Context, } if fn.HasDuplicates(wireOutpoints) { - return nil, fmt.Errorf("selected outpoints contain " + + return nil, errors.New("selected outpoints contain " + "duplicate values") } @@ -1400,7 +1400,7 @@ func (r *rpcServer) SendCoins(ctx context.Context, // At this point, the amount shouldn't be set since we've been // instructed to sweep all the coins from the wallet. if in.Amount != 0 { - return nil, fmt.Errorf("amount set while SendAll is " + + return nil, errors.New("amount set while SendAll is " + "active") } diff --git a/server.go b/server.go index ecb3eceae..bd877a7ad 100644 --- a/server.go +++ b/server.go @@ -572,7 +572,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr, if cfg.ProtocolOptions.TaprootOverlayChans && implCfg.AuxFundingController.IsNone() { - return nil, fmt.Errorf("taproot overlay flag set, but not " + + return nil, errors.New("taproot overlay flag set, but not " + "aux controllers") } @@ -1428,7 +1428,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr, if ourPolicy == nil { // Something is wrong, so return an error. - return nil, fmt.Errorf("we don't have an edge") + return nil, errors.New("we don't have an edge") } err = s.graphDB.DeleteChannelEdges(