Skip to content

Commit

Permalink
Merge pull request btcsuite#913 from guggero/wrap-errors
Browse files Browse the repository at this point in the history
Wrapp all errors
  • Loading branch information
Roasbeef authored and buck54321 committed Apr 21, 2024
1 parent 8b1ad3b commit 12af59c
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 67 deletions.
20 changes: 10 additions & 10 deletions chain/bitcoind_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ func (c *BitcoindClient) NotifyBlocks() error {
bestHash, bestHeight, err := c.GetBestBlock()
if err != nil {
atomic.StoreUint32(&c.notifyBlocks, 0)
return fmt.Errorf("unable to retrieve best block: %v", err)
return fmt.Errorf("unable to retrieve best block: %w", err)
}
bestHeader, err := c.GetBlockHeaderVerbose(bestHash)
if err != nil {
atomic.StoreUint32(&c.notifyBlocks, 0)
return fmt.Errorf("unable to retrieve header for best block: "+
"%v", err)
"%w", err)
}

c.bestBlockMtx.Lock()
Expand Down Expand Up @@ -507,12 +507,12 @@ func (c *BitcoindClient) Start() error {
// Retrieve the best block of the chain.
bestHash, bestHeight, err := c.GetBestBlock()
if err != nil {
return fmt.Errorf("unable to retrieve best block: %v", err)
return fmt.Errorf("unable to retrieve best block: %w", err)
}
bestHeader, err := c.GetBlockHeaderVerbose(bestHash)
if err != nil {
return fmt.Errorf("unable to retrieve header for best block: "+
"%v", err)
"%w", err)
}

c.bestBlockMtx.Lock()
Expand Down Expand Up @@ -839,7 +839,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
bestHash := reorgBlock.BlockHash()
bestHeight, err := c.GetBlockHeight(&bestHash)
if err != nil {
return fmt.Errorf("unable to get block height for %v: %v",
return fmt.Errorf("unable to get block height for %v: %w",
bestHash, err)
}

Expand All @@ -861,7 +861,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
for i := bestHeight - 1; i >= currentBlock.Height; i-- {
block, err := c.GetBlock(&previousBlock)
if err != nil {
return fmt.Errorf("unable to get block %v: %v",
return fmt.Errorf("unable to get block %v: %w",
previousBlock, err)
}
blocksToNotify.PushFront(block)
Expand All @@ -875,7 +875,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
// We'll start by retrieving the header to the best block known to us.
currentHeader, err := c.GetBlockHeader(&currentBlock.Hash)
if err != nil {
return fmt.Errorf("unable to get block header for %v: %v",
return fmt.Errorf("unable to get block header for %v: %w",
currentBlock.Hash, err)
}

Expand All @@ -898,7 +898,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
prevBlock := &currentHeader.PrevBlock
currentHeader, err = c.GetBlockHeader(prevBlock)
if err != nil {
return fmt.Errorf("unable to get block header for %v: %v",
return fmt.Errorf("unable to get block header for %v: %w",
prevBlock, err)
}

Expand All @@ -910,7 +910,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
// once we've found our common ancestor.
block, err := c.GetBlock(&previousBlock)
if err != nil {
return fmt.Errorf("unable to get block %v: %v",
return fmt.Errorf("unable to get block %v: %w",
previousBlock, err)
}
blocksToNotify.PushFront(block)
Expand All @@ -936,7 +936,7 @@ func (c *BitcoindClient) reorg(currentBlock waddrmgr.BlockStamp,
nextHash := nextBlock.BlockHash()
nextHeader, err := c.GetBlockHeader(&nextHash)
if err != nil {
return fmt.Errorf("unable to get block header for %v: %v",
return fmt.Errorf("unable to get block header for %v: %w",
nextHash, err)
}

Expand Down
2 changes: 1 addition & 1 deletion chain/bitcoind_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func NewBitcoindConn(cfg *BitcoindConfig) (*BitcoindConn, error) {
chainInfo, err := client.GetBlockChainInfo()
if err != nil {
return nil, fmt.Errorf("unable to determine if bitcoind is "+
"pruned: %v", err)
"pruned: %w", err)
}

// Only initialize the PrunedBlockDispatcher when the connected bitcoind
Expand Down
4 changes: 2 additions & 2 deletions chain/bitcoind_zmq_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func newBitcoindZMQEvents(cfg *ZMQConfig, client *rpcclient.Client,
)
if err != nil {
return nil, fmt.Errorf("unable to subscribe for zmq block "+
"events: %v", err)
"events: %w", err)
}

zmqTxConn, err := gozmq.Subscribe(
Expand All @@ -138,7 +138,7 @@ func newBitcoindZMQEvents(cfg *ZMQConfig, client *rpcclient.Client,
}

return nil, fmt.Errorf("unable to subscribe for zmq tx "+
"events: %v", err)
"events: %w", err)
}

// Create the config for mempool and attach default values if not
Expand Down
2 changes: 1 addition & 1 deletion chain/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (m *mempool) batchGetRawTxes(txids []*chainhash.Hash,
// Ask the client to send all the batched requests.
err := m.cfg.client.Send()
if err != nil {
return fmt.Errorf("Send GetRawTransaction got %v", err)
return fmt.Errorf("Send GetRawTransaction got %w", err)
}

// Iterate the recievers and fetch the response.
Expand Down
6 changes: 3 additions & 3 deletions chain/neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *NeutrinoClient) BackEnd() string {
// Start replicates the RPC client's Start method.
func (s *NeutrinoClient) Start() error {
if err := s.CS.Start(); err != nil {
return fmt.Errorf("error starting chain service: %v", err)
return fmt.Errorf("error starting chain service: %w", err)
}

s.clientMtx.Lock()
Expand Down Expand Up @@ -407,11 +407,11 @@ func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []ltcutil.Addre

bestBlock, err := s.CS.BestBlock()
if err != nil {
return fmt.Errorf("can't get chain service's best block: %s", err)
return fmt.Errorf("can't get chain service's best block: %w", err)
}
header, err := s.CS.GetBlockHeader(&bestBlock.Hash)
if err != nil {
return fmt.Errorf("can't get block header for hash %v: %s",
return fmt.Errorf("can't get block header for hash %v: %w",
bestBlock.Hash, err)
}

Expand Down
2 changes: 1 addition & 1 deletion chain/pruned_block_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (d *PrunedBlockDispatcher) connectToPeer(addr string) (bool, error) {
peer, err := d.newQueryPeer(addr)
if err != nil {
return true, fmt.Errorf("unable to configure query peer %v: "+
"%v", addr, err)
"%w", addr, err)
}

// Establish the connection and wait for the protocol negotiation to
Expand Down
2 changes: 1 addition & 1 deletion cmd/sweepaccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func fatalf(format string, args ...interface{}) {
}

func errContext(err error, context string) error {
return fmt.Errorf("%s: %v", context, err)
return fmt.Errorf("%s: %w", context, err)
}

// Flags.
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func loadConfig() (*config, []string, error) {

// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", "loadConfig", err.Error())
err := fmt.Errorf("%s: %w", "loadConfig", err)
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/legacy/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func chainedPubKey(pubkey, chaincode []byte) ([]byte, error) {
var xorBytesScalar btcec.ModNScalar
overflow := xorBytesScalar.SetBytes(&xorbytes)
if overflow != 0 {
return nil, fmt.Errorf("unable to create pubkey: %v", err)
return nil, fmt.Errorf("unable to create pubkey due to overflow")
}

var (
Expand Down
4 changes: 2 additions & 2 deletions rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,12 @@ func makeOutputs(pairs map[string]ltcutil.Amount, chainParams *chaincfg.Params)
for addrStr, amt := range pairs {
addr, err := ltcutil.DecodeAddress(addrStr, chainParams)
if err != nil {
return nil, fmt.Errorf("cannot decode address: %s", err)
return nil, fmt.Errorf("cannot decode address: %w", err)
}

pkScript, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, fmt.Errorf("cannot create txout script: %s", err)
return nil, fmt.Errorf("cannot create txout script: %w", err)
}

outputs = append(outputs, wire.NewTxOut(int64(amt), pkScript))
Expand Down
2 changes: 1 addition & 1 deletion waddrmgr/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ func newWitnessScriptAddress(m *ScopedKeyManager, account uint32, scriptIdent,
tweakedPubKey, err := schnorr.ParsePubKey(scriptIdent)
if err != nil {
return nil, fmt.Errorf("error lifting public key from "+
"script ident: %v", err)
"script ident: %w", err)
}

return &taprootScriptAddress{
Expand Down
2 changes: 1 addition & 1 deletion waddrmgr/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestStoreMaxReorgDepth(t *testing.T) {
firstBlock := blocks[0]
_, err = fetchBlockHash(ns, firstBlock.Height)
if !IsError(err, ErrBlockNotFound) {
return fmt.Errorf("expected ErrBlockNotFound, got %v",
return fmt.Errorf("expected ErrBlockNotFound, got %w",
err)
}

Expand Down
9 changes: 7 additions & 2 deletions waddrmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (e ErrorCode) String() string {
// manager operation. It is used to indicate several types of failures
// including errors with caller requests such as invalid accounts or requesting
// private keys against a locked address manager, errors with the database
// (ErrDatabase), errors with key chain derivation (ErrKeyChain), and errors
// (ErrDatabase), errors with keychain derivation (ErrKeyChain), and errors
// related to crypto (ErrCrypto).
//
// The caller can use type assertions to determine if an error is a ManagerError
Expand All @@ -195,7 +195,7 @@ func (e ErrorCode) String() string {
// Err field set with the underlying error.
type ManagerError struct {
ErrorCode ErrorCode // Describes the kind of error
Description string // Human readable description of the issue
Description string // Human-readable description of the issue
Err error // Underlying error
}

Expand All @@ -207,6 +207,11 @@ func (e ManagerError) Error() string {
return e.Description
}

// Unwrap returns the underlying error, if any.
func (e ManagerError) Unwrap() error {
return e.Err
}

// managerError creates a ManagerError given a set of arguments.
func managerError(c ErrorCode, desc string, err error) ManagerError {
return ManagerError{ErrorCode: c, Description: desc, Err: err}
Expand Down
2 changes: 1 addition & 1 deletion waddrmgr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ func TestScopedKeyManagerManagement(t *testing.T) {

_, err := mgr.Address(ns, lastAddr.Address())
if err != nil {
return fmt.Errorf("unable to find addr: %v", err)
return fmt.Errorf("unable to find addr: %w", err)
}

err = mgr.MarkUsed(ns, lastAddr.Address())
Expand Down
4 changes: 2 additions & 2 deletions waddrmgr/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func populateBirthdayBlock(ns walletdb.ReadWriteBucket) error {
// We'll start by fetching our birthday timestamp.
birthdayTimestamp, err := fetchBirthday(ns)
if err != nil {
return fmt.Errorf("unable to fetch birthday timestamp: %v", err)
return fmt.Errorf("unable to fetch birthday timestamp: %w", err)
}

log.Infof("Setting the wallet's birthday block from timestamp=%v",
Expand All @@ -301,7 +301,7 @@ func populateBirthdayBlock(ns walletdb.ReadWriteBucket) error {
// the corresponding chain.
genesisHash, err := fetchBlockHash(ns, 0)
if err != nil {
return fmt.Errorf("unable to fetch genesis block hash: %v", err)
return fmt.Errorf("unable to fetch genesis block hash: %w", err)
}

var genesisTimestamp time.Time
Expand Down
6 changes: 3 additions & 3 deletions waddrmgr/scoped_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func (s *ScopedKeyManager) AccountProperties(ns walletdb.ReadBucket,
)
if err != nil {
return nil, fmt.Errorf("failed to retrieve "+
"account public key: %v", err)
"account public key: %w", err)
}
}
} else {
Expand Down Expand Up @@ -2210,12 +2210,12 @@ func (s *ScopedKeyManager) ImportTaprootScript(ns walletdb.ReadWriteBucket,
// tweak the taproot key.
taprootKey, err := tapscript.TaprootKey()
if err != nil {
return nil, fmt.Errorf("error calculating script root: %v", err)
return nil, fmt.Errorf("error calculating script root: %w", err)
}

script, err := tlvEncodeTaprootScript(tapscript)
if err != nil {
return nil, fmt.Errorf("error encoding taproot script: %v", err)
return nil, fmt.Errorf("error encoding taproot script: %w", err)
}

managedAddr, err := s.importScriptAddress(
Expand Down
6 changes: 3 additions & 3 deletions waddrmgr/tlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func tlvEncodeTaprootScript(s *Tapscript) ([]byte, error) {
blockBytes, err := s.ControlBlock.ToBytes()
if err != nil {
return nil, fmt.Errorf("error encoding control block: "+
"%v", err)
"%w", err)
}
tlvRecords = append(tlvRecords, tlv.MakePrimitiveRecord(
typeTapscriptControlBlock, &blockBytes,
Expand Down Expand Up @@ -141,15 +141,15 @@ func tlvDecodeTaprootTaprootScript(tlvData []byte) (*Tapscript, error) {
)
if err != nil {
return nil, fmt.Errorf("error decoding control block: "+
"%v", err)
"%w", err)
}
}

if t, ok := parsedTypes[typeTapscriptFullOutputKey]; ok && t == nil {
s.FullOutputKey, err = schnorr.ParsePubKey(fullOutputKeyBytes)
if err != nil {
return nil, fmt.Errorf("error decoding full output "+
"key: %v", err)
"key: %w", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ func validateMsgTx(tx *wire.MsgTx, prevScripts [][]byte, inputValues []ltcutil.A
txscript.StandardVerifyFlags, nil, hashCache,
int64(inputValues[i]), inputFetcher)
if err != nil {
return fmt.Errorf("cannot create script engine: %s", err)
return fmt.Errorf("cannot create script engine: %w", err)
}
err = vm.Execute()
if err != nil {
return fmt.Errorf("cannot validate transaction: %s", err)
return fmt.Errorf("cannot validate transaction: %w", err)
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions wallet/disksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func checkCreateDir(path string) error {
if os.IsNotExist(err) {
// Attempt data directory creation
if err = os.MkdirAll(path, 0700); err != nil {
return fmt.Errorf("cannot create directory: %s", err)
return fmt.Errorf("cannot create directory: %w", err)
}
} else {
return fmt.Errorf("error checking directory: %s", err)
return fmt.Errorf("error checking directory: %w", err)
}
} else {
if !fi.IsDir() {
Expand Down
2 changes: 1 addition & 1 deletion wallet/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func DropTransactionHistory(db walletdb.DB, keepLabels bool) error {
return waddrmgr.PutBirthdayBlock(ns, birthdayBlock)
})
if err != nil {
return fmt.Errorf("failed to drop and re-create namespace: %v",
return fmt.Errorf("failed to drop and re-create namespace: %w",
err)
}

Expand Down
6 changes: 3 additions & 3 deletions wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (w *Wallet) ImportPublicKey(pubKey *btcec.PublicKey,
err = w.chainClient.NotifyReceived([]ltcutil.Address{addr.Address()})
if err != nil {
return fmt.Errorf("unable to subscribe for address "+
"notifications: %v", err)
"notifications: %w", err)
}

return nil
Expand Down Expand Up @@ -474,7 +474,7 @@ func (w *Wallet) ImportTaprootScript(scope waddrmgr.KeyScope,
err = w.chainClient.NotifyReceived([]ltcutil.Address{addr.Address()})
if err != nil {
return nil, fmt.Errorf("unable to subscribe for address "+
"notifications: %v", err)
"notifications: %w", err)
}

return addr, nil
Expand Down Expand Up @@ -571,7 +571,7 @@ func (w *Wallet) ImportPrivateKey(scope waddrmgr.KeyScope, wif *ltcutil.WIF,
err := w.chainClient.NotifyReceived([]ltcutil.Address{addr})
if err != nil {
return "", fmt.Errorf("failed to subscribe for address ntfns for "+
"address %s: %s", addr.EncodeAddress(), err)
"address %s: %w", addr.EncodeAddress(), err)
}
}

Expand Down
Loading

0 comments on commit 12af59c

Please sign in to comment.