Skip to content
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

Wrapp all errors #913

Merged
merged 6 commits into from
Mar 9, 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
20 changes: 10 additions & 10 deletions chain/bitcoind_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,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 @@ -517,12 +517,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 @@ -849,7 +849,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 @@ -871,7 +871,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 @@ -885,7 +885,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 @@ -908,7 +908,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 @@ -920,7 +920,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 @@ -946,7 +946,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 @@ -123,7 +123,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 @@ -137,7 +137,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 @@ -103,7 +103,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 @@ -426,11 +426,11 @@ func (s *NeutrinoClient) Rescan(startHash *chainhash.Hash, addrs []btcutil.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
10 changes: 5 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ func parseAndSetDebugLevels(debugLevel string) error {
// line options.
//
// The configuration proceeds as follows:
// 1) Start with a default config with sane settings
// 2) Pre-parse the command line to check for an alternative config file
// 3) Load configuration file overwriting defaults with any specified options
// 4) Parse CLI options and overwrite/add any specified options
// 1. Start with a default config with sane settings
// 2. Pre-parse the command line to check for an alternative config file
// 3. Load configuration file overwriting defaults with any specified options
// 4. Parse CLI options and overwrite/add any specified options
//
// The above results in btcwallet functioning properly without any config
// settings while still allowing the user to override settings with config files
Expand Down 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 @@ -234,7 +234,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
40 changes: 23 additions & 17 deletions rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,14 +1073,17 @@ func listLockUnspent(icmd interface{}, w *wallet.Wallet) (interface{}, error) {

// listReceivedByAccount handles a listreceivedbyaccount request by returning
// a slice of objects, each one containing:
// "account": the receiving account;
// "amount": total amount received by the account;
// "confirmations": number of confirmations of the most recent transaction.
//
// "account": the receiving account;
// "amount": total amount received by the account;
// "confirmations": number of confirmations of the most recent transaction.
//
// It takes two parameters:
// "minconf": minimum number of confirmations to consider a transaction -
// default: one;
// "includeempty": whether or not to include addresses that have no transactions -
// default: false.
//
// "minconf": minimum number of confirmations to consider a transaction -
// default: one;
// "includeempty": whether or not to include addresses that have no transactions -
// default: false.
func listReceivedByAccount(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
cmd := icmd.(*btcjson.ListReceivedByAccountCmd)

Expand All @@ -1104,15 +1107,18 @@ func listReceivedByAccount(icmd interface{}, w *wallet.Wallet) (interface{}, err

// listReceivedByAddress handles a listreceivedbyaddress request by returning
// a slice of objects, each one containing:
// "account": the account of the receiving address;
// "address": the receiving address;
// "amount": total amount received by the address;
// "confirmations": number of confirmations of the most recent transaction.
//
// "account": the account of the receiving address;
// "address": the receiving address;
// "amount": total amount received by the address;
// "confirmations": number of confirmations of the most recent transaction.
//
// It takes two parameters:
// "minconf": minimum number of confirmations to consider a transaction -
// default: one;
// "includeempty": whether or not to include addresses that have no transactions -
// default: false.
//
// "minconf": minimum number of confirmations to consider a transaction -
// default: one;
// "includeempty": whether or not to include addresses that have no transactions -
// default: false.
func listReceivedByAddress(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
cmd := icmd.(*btcjson.ListReceivedByAddressCmd)

Expand Down Expand Up @@ -1360,12 +1366,12 @@ func makeOutputs(pairs map[string]btcutil.Amount, chainParams *chaincfg.Params)
for addrStr, amt := range pairs {
addr, err := btcutil.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 @@ -1017,7 +1017,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 {
guggero marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -615,7 +615,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 @@ -2170,12 +2170,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
Loading
Loading