Simplify error handling with waddrmgr.IsError.

This commit is contained in:
Josh Rickmar 2015-05-27 18:21:17 -04:00
parent 39cab6087a
commit 9d5abaf14e
3 changed files with 11 additions and 49 deletions

View file

@ -172,41 +172,6 @@ func (c *websocketClient) send(b []byte) error {
}
}
// isManagerLockedError returns whether or not the passed error is due to the
// address manager being locked.
func isManagerLockedError(err error) bool {
merr, ok := err.(waddrmgr.ManagerError)
return ok && merr.ErrorCode == waddrmgr.ErrLocked
}
// isManagerWrongPassphraseError returns whether or not the passed error is due
// to the address manager being provided with an invalid passprhase.
func isManagerWrongPassphraseError(err error) bool {
merr, ok := err.(waddrmgr.ManagerError)
return ok && merr.ErrorCode == waddrmgr.ErrWrongPassphrase
}
// isManagerDuplicateAddressError returns whether or not the passed error is due to a
// duplicate item being provided to the address manager.
func isManagerDuplicateAddressError(err error) bool {
merr, ok := err.(waddrmgr.ManagerError)
return ok && merr.ErrorCode == waddrmgr.ErrDuplicateAddress
}
// isManagerAddressNotFoundError returns whether or not the passed error is due to a
// the address not being found.
func isManagerAddressNotFoundError(err error) bool {
merr, ok := err.(waddrmgr.ManagerError)
return ok && merr.ErrorCode == waddrmgr.ErrAddressNotFound
}
// isManagerAccountNotFoundError returns whether or not the passed error is due
// to the account not being found.
func isManagerAccountNotFoundError(err error) bool {
merr, ok := err.(waddrmgr.ManagerError)
return ok && merr.ErrorCode == waddrmgr.ErrAccountNotFound
}
// parseListeners splits the list of listen addresses passed in addrs into
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
// listeners on the correct interface "tcp4" and "tcp6". It also properly
@ -1592,7 +1557,7 @@ func DumpPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (in
}
key, err := w.DumpWIFPrivateKey(addr)
if isManagerLockedError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
// Address was found, but the private key isn't
// accessible.
return nil, &ErrWalletUnlockNeeded
@ -1605,7 +1570,7 @@ func DumpPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (in
// TODO: finish this to match bitcoind by writing the dump to a file.
func DumpWallet(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (interface{}, error) {
keys, err := w.DumpPrivKeys()
if isManagerLockedError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
return nil, &ErrWalletUnlockNeeded
}
@ -1848,10 +1813,10 @@ func ImportPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (
// Import the private key, handling any errors.
_, err = w.ImportPrivateKey(wif, nil, *cmd.Rescan)
switch {
case isManagerDuplicateAddressError(err):
case waddrmgr.IsError(err, waddrmgr.ErrDuplicateAddress):
// Do not return duplicate key errors to the client.
return nil, nil
case isManagerLockedError(err):
case waddrmgr.IsError(err, waddrmgr.ErrLocked):
return nil, &ErrWalletUnlockNeeded
}
@ -1893,7 +1858,7 @@ func CreateNewAccount(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}
}
_, err = w.Manager.NewAccount(cmd.Account)
if isManagerLockedError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCWalletUnlockNeeded,
Message: "Creating an account requires the wallet to be unlocked. " +
@ -2599,7 +2564,7 @@ func sendPairs(w *wallet.Wallet, amounts map[string]btcutil.Amount,
if err == wallet.ErrNonPositiveAmount {
return "", ErrNeedPositiveAmount
}
if isManagerLockedError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
return "", &ErrWalletUnlockNeeded
}
switch err.(type) {
@ -3111,7 +3076,7 @@ func ValidateAddress(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{})
ainfo, err := w.Manager.Address(addr)
if err != nil {
if isManagerAddressNotFoundError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
// No additional information available about the address.
return result, nil
}
@ -3251,7 +3216,7 @@ func WalletPassphraseChange(w *wallet.Wallet, chainSvr *chain.Client, icmd inter
err := w.ChangePassphrase([]byte(cmd.OldPassphrase),
[]byte(cmd.NewPassphrase))
if isManagerWrongPassphraseError(err) {
if waddrmgr.IsError(err, waddrmgr.ErrWrongPassphrase) {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCWalletPassphraseIncorrect,
Message: "Incorrect passphrase",

View file

@ -191,8 +191,7 @@ func (w *Wallet) addRelevantTx(rec *wtxmgr.TxRecord, block *wtxmgr.BlockMeta) er
// Missing addresses are skipped. Other errors should
// be propagated.
code := err.(waddrmgr.ManagerError).ErrorCode
if code != waddrmgr.ErrAddressNotFound {
if !waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
return err
}
}

View file

@ -752,8 +752,7 @@ func (w *Wallet) CurrentAddress(account uint32) (btcutil.Address, error) {
addr, err := w.Manager.LastExternalAddress(account)
if err != nil {
// If no address exists yet, create the first external address
merr, ok := err.(waddrmgr.ManagerError)
if ok && merr.ErrorCode == waddrmgr.ErrAddressNotFound {
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
return w.NewAddress(account)
}
return nil, err
@ -1349,8 +1348,7 @@ func (w *Wallet) ExportWatchingWallet(pubPass string) (string, error) {
// Only return the error is it's not because it's already
// watching-only. When it is already watching-only, the code
// just falls through to the export below.
if merr, ok := err.(waddrmgr.ManagerError); ok &&
merr.ErrorCode != waddrmgr.ErrWatchingOnly {
if !waddrmgr.IsError(err, waddrmgr.ErrWatchingOnly) {
return "", err
}
}