From adf7fb2131d4f61bb9489475a38628a994e6fd57 Mon Sep 17 00:00:00 2001 From: LesCyber Date: Sun, 2 Mar 2025 22:20:38 +0800 Subject: [PATCH] refactor: unify the error handling methods that are different from the project style Signed-off-by: LesCyber --- funding/manager.go | 5 +++-- graph/db/graph.go | 4 ++-- lnrpc/websocket_proxy.go | 3 ++- peer/brontide.go | 2 +- rpcserver.go | 4 ++-- server.go | 3 ++- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/funding/manager.go b/funding/manager.go index 395cccb2a..975f6d82e 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -3,6 +3,7 @@ package funding import ( "bytes" "encoding/binary" + "errors" "fmt" "io" "sync" @@ -1112,7 +1113,7 @@ func (f *Manager) advanceFundingState(channel *channeldb.OpenChannel, channelState, shortChanID, err := f.getChannelOpeningState( &channel.FundingOutpoint, ) - if err == channeldb.ErrChannelNotFound { + if errors.Is(err, channeldb.ErrChannelNotFound) { // Channel not in fundingManager's opening database, // meaning it was successfully announced to the // network. @@ -1326,7 +1327,7 @@ func (f *Manager) advancePendingChannelState(channel *channeldb.OpenChannel, } confChannel, err := f.waitForFundingWithTimeout(channel) - if err == ErrConfirmationTimeout { + if errors.Is(err, ErrConfirmationTimeout) { return f.fundingTimeout(channel, pendingChanID) } else if err != nil { return fmt.Errorf("error waiting for funding "+ diff --git a/graph/db/graph.go b/graph/db/graph.go index 0a643144e..4d8f87d9b 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -1076,7 +1076,7 @@ func (c *ChannelGraph) AddChannelEdge(edge *models.ChannelEdgeInfo, // Silence ErrEdgeAlreadyExist so that the batch can // succeed, but propagate the error via local state. - if err == ErrEdgeAlreadyExist { + if errors.Is(err, ErrEdgeAlreadyExist) { alreadyExists = true return nil } @@ -3493,7 +3493,7 @@ func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) ( policy1 = nil policy2 = nil }) - if err == ErrZombieEdge { + if errors.Is(err, ErrZombieEdge) { return edgeInfo, nil, nil, err } if err != nil { diff --git a/lnrpc/websocket_proxy.go b/lnrpc/websocket_proxy.go index b333ba1c8..f1ded4ed0 100644 --- a/lnrpc/websocket_proxy.go +++ b/lnrpc/websocket_proxy.go @@ -5,6 +5,7 @@ package lnrpc import ( "bufio" + "errors" "io" "net/http" "net/textproto" @@ -500,7 +501,7 @@ func IsClosedConnError(err error) bool { if err == nil { return false } - if err == http.ErrServerClosed { + if errors.Is(err, http.ErrServerClosed) { return true } diff --git a/peer/brontide.go b/peer/brontide.go index f0399b4e8..596d5fe16 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -4001,7 +4001,7 @@ func (p *Brontide) handleCloseMsg(msg *closeMsg) { chanCloser, err := p.fetchActiveChanCloser(msg.cid) if err != nil { // If the channel is not known to us, we'll simply ignore this message. - if err == ErrChannelNotFound { + if errors.Is(err, ErrChannelNotFound) { return } diff --git a/rpcserver.go b/rpcserver.go index 3291b7d38..8858a37f0 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1440,7 +1440,7 @@ func (r *rpcServer) SendCoins(ctx context.Context, // If sending everything to this address would invalidate our // reserved wallet balance, we create a new sweep tx, where // we'll send the reserved value back to our wallet. - if err == lnwallet.ErrReservedValueInvalidated { + if errors.Is(err, lnwallet.ErrReservedValueInvalidated) { sweepTxPkg.CancelSweepAttempt() rpcsLog.Debugf("Reserved value %v not satisfied after "+ @@ -4348,7 +4348,7 @@ func (r *rpcServer) nurseryPopulateForceCloseResp(chanPoint *wire.OutPoint, // didn't have any time-locked outputs, then the nursery may not know of // the contract. nurseryInfo, err := r.server.utxoNursery.NurseryReport(chanPoint) - if err == contractcourt.ErrContractNotFound { + if errors.Is(err, contractcourt.ErrContractNotFound) { return nil } if err != nil { diff --git a/server.go b/server.go index bd877a7ad..b54556054 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "context" "crypto/rand" "encoding/hex" + "errors" "fmt" "math/big" prand "math/rand" @@ -4906,7 +4907,7 @@ func (s *server) DisconnectPeer(pubKey *btcec.PublicKey) error { // exit in an error as we can't disconnect from a peer that we're not // currently connected to. peer, err := s.findPeerByPubStr(pubStr) - if err == ErrPeerNotConnected { + if errors.Is(err, ErrPeerNotConnected) { return fmt.Errorf("peer %x is not connected", pubBytes) }