Skip to content

Commit

Permalink
accounts: return consistent error for duplicate label
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Jan 14, 2025
1 parent 6910512 commit 04ca82e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions accounts/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package accounts

import "errors"

var (
// ErrLabelAlreadyExists is returned by the CreateAccount method if the
// account label is already used by an existing account.
ErrLabelAlreadyExists = errors.New(
"account label uniqueness constraint violation",
)
)
3 changes: 2 additions & 1 deletion accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ func (s *BoltStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
for _, account := range accounts {
if account.Label == label {
return nil, fmt.Errorf("an account with the "+
"label '%s' already exists", label)
"label '%s' already exists: %w", label,
ErrLabelAlreadyExists)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAccountStore(t *testing.T) {

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

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

0 comments on commit 04ca82e

Please # to comment.