Skip to content

Commit d4559c3

Browse files
committed
backend/keystore: refactor errors using error codes
1 parent ddff05f commit d4559c3

File tree

6 files changed

+20
-25
lines changed

6 files changed

+20
-25
lines changed

backend/accounts.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
697697
Action: action.Replace,
698698
Object: data{
699699
Type: "error",
700-
ErrorCode: "wrongKeystore",
701-
ErrorMessage: err.Error(),
700+
ErrorCode: err.Error(),
701+
ErrorMessage: "",
702702
},
703703
})
704704
c := make(chan bool)
@@ -712,7 +712,7 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
712712
select {
713713
case retry := <-c:
714714
if !retry {
715-
err = ErrUserAbort
715+
err = errp.ErrUserAbort
716716
break outerLoop
717717
}
718718
case <-time.After(timeout):
@@ -727,7 +727,7 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
727727
// If a previous connect-keystore request is in progress, the previous request is
728728
// failed, but we don't dismiss the prompt, as the new prompt has already been shown
729729
// by the above "connect" notification.
730-
case err == nil || errp.Cause(err) == ErrUserAbort:
730+
case err == nil || errp.Cause(err) == errp.ErrUserAbort:
731731
// Dismiss prompt after success or upon user abort.
732732
backend.Notify(observable.Event{
733733
Subject: "connect-keystore",
@@ -737,7 +737,7 @@ func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *
737737
default:
738738
var errorCode = ""
739739
if errp.Cause(err) == errTimeout {
740-
errorCode = "timeout"
740+
errorCode = err.Error()
741741
}
742742
// Display error to user.
743743
backend.Notify(observable.Event{

backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ func (backend *Backend) GetAccountFromCode(acctCode accountsTypes.Code) (account
891891

892892
// CancelConnectKeystore cancels a pending keystore connection request if one exists.
893893
func (backend *Backend) CancelConnectKeystore() {
894-
backend.connectKeystore.cancel(ErrUserAbort)
894+
backend.connectKeystore.cancel(errp.ErrUserAbort)
895895
}
896896

897897
// SetWatchonly sets the keystore's watchonly flag to `watchonly`.

backend/coins/btc/handlers/handlers.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ type Handlers struct {
5151
log *logrus.Entry
5252
}
5353

54-
const (
55-
// ErrUserAbort is returned if the user aborted the current operation.
56-
errUserAbort errp.ErrorCode = "userAbort"
57-
)
58-
5954
// NewHandlers creates a new Handlers instance.
6055
func NewHandlers(
6156
handleFunc func(string, func(*http.Request) (interface{}, error)) *mux.Route, log *logrus.Entry) *Handlers {
@@ -739,10 +734,10 @@ func (handlers *Handlers) postSignBTCAddress(r *http.Request) (interface{}, erro
739734
request.Format)
740735
if err != nil {
741736
if firmware.IsErrorAbort(err) {
742-
return response{Success: false, ErrorCode: string(errUserAbort)}, nil
737+
return response{Success: false, ErrorCode: errp.ErrUserAbort.Error()}, nil
743738
}
744739
if errp.Cause(err) == backend.ErrWrongKeystore {
745-
return response{Success: false, ErrorCode: string("wrongKeystore")}, nil
740+
return response{Success: false, ErrorCode: backend.ErrWrongKeystore.Error()}, nil
746741
}
747742

748743
handlers.log.WithField("code", account.Config().Config.Code).Error(err)

backend/connectkeystore.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@ package backend
1717
import (
1818
"bytes"
1919
"context"
20-
"errors"
2120
"time"
2221

2322
"github.com/digitalbitbox/bitbox-wallet-app/backend/keystore"
23+
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
2424
"github.com/digitalbitbox/bitbox-wallet-app/util/locker"
2525
)
2626

27-
// ErrWrongKeystore is returned if the connected keystore is not the expected one.
28-
var ErrWrongKeystore = errors.New("Wrong device/keystore connected.")
27+
const (
28+
// ErrWrongKeystore is returned if the connected keystore is not the expected one.
29+
ErrWrongKeystore errp.ErrorCode = "wrongKeystore"
2930

30-
// ErrUserAbort is raised when a keystore connection is aborted calling CancelConnectKeystore().
31-
var ErrUserAbort = errors.New("aborted by user")
32-
var errReplaced = errors.New("replaced by new prompt")
33-
34-
var errTimeout = errors.New("timeout")
31+
errTimeout errp.ErrorCode = "timeout"
32+
errReplaced errp.ErrorCode = "connectReplaced"
33+
)
3534

3635
// connectKeystore is a helper struct to enable connecting to a keystore with a specific root
3736
// fingerprint.

backend/connectkeystore_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
keystoremock "github.com/digitalbitbox/bitbox-wallet-app/backend/keystore/mocks"
23+
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
2324

2425
"github.com/stretchr/testify/require"
2526
)
@@ -52,10 +53,10 @@ func TestConnectKeystore(t *testing.T) {
5253
go func() {
5354
defer wg.Done()
5455
time.Sleep(50 * time.Millisecond)
55-
ck.cancel(ErrUserAbort)
56+
ck.cancel(errp.ErrUserAbort)
5657
}()
5758
_, err := ck.connect(nil, fingerprint, time.Second)
58-
require.Equal(t, ErrUserAbort, err)
59+
require.Equal(t, errp.ErrUserAbort, err)
5960
wg.Wait()
6061
})
6162

util/errp/errp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ func (e ErrorCode) Error() string {
6565
// The follwing error codes are defined here because they are shared between packages.
6666
// Package specific error codes should be defined inside the package itself.
6767
const (
68-
// ErrUserAbort is returned if the user aborted the current operation.
69-
// This is just an example: ErrUserAbort ErrorCode = "userAbort"
68+
// ErrUserAbort is returned if the user aborted the current operation.
69+
ErrUserAbort ErrorCode = "userAbort"
7070
)

0 commit comments

Comments
 (0)