Skip to content

Commit 7fa370e

Browse files
committed
accounts: return consistent error for duplicate label
In preparation for when we have a SQL DB implementation, we want our unit tests to run smoothly against all DB backends and have the same results. To achieve this, we need to turn some errors into global error variables that can be matched against instead. In this commit, we do this for the unique constraint violation of the account label.
1 parent 9f08daf commit 7fa370e

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

accounts/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package accounts
2+
3+
import "errors"
4+
5+
var (
6+
// ErrLabelAlreadyExists is returned by the CreateAccount method if the
7+
// account label is already used by an existing account.
8+
ErrLabelAlreadyExists = errors.New(
9+
"account label uniqueness constraint violation",
10+
)
11+
)

accounts/store_kvdb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ func (s *BoltStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
133133
for _, account := range accounts {
134134
if account.Label == label {
135135
return nil, fmt.Errorf("an account with the "+
136-
"label '%s' already exists", label)
136+
"label '%s' already exists: %w", label,
137+
ErrLabelAlreadyExists)
137138
}
138139
}
139140
}

accounts/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAccountStore(t *testing.T) {
2929

3030
// Make sure we cannot create a second account with the same label.
3131
_, err = store.NewAccount(ctx, 123, time.Time{}, "foo")
32-
require.ErrorContains(t, err, "account with the label 'foo' already")
32+
require.ErrorIs(t, err, ErrLabelAlreadyExists)
3333

3434
// Make sure we cannot set a label that looks like an account ID.
3535
_, err = store.NewAccount(ctx, 123, time.Time{}, "0011223344556677")

0 commit comments

Comments
 (0)