ICU-738/Refactor internal/db to domain errors (#815)

* Refactor internal/db to domain errors
This commit is contained in:
Louis Ruch authored and GitHub committed 2020-12-21 10:07:34 -08:00
1 parent b5d84495a3
commit bfbb179741
82 files changed
+742 -640

No files matched your search

+6 -5
View File
@@ -1,21 +1,22 @@
package password
import "errors"
import "github.com/hashicorp/boundary/internal/errors"
// TODO: remove these errors once all code has been refactored to creating inline domain errors
var (
// ErrTooShort results from attempting to set a password which is to
// short.
ErrTooShort = errors.New("too short")
ErrTooShort = errors.E(errors.WithCode(errors.PasswordTooShort))
// ErrUnsupportedConfiguration results from attempting to perform an
// operation that sets a password configuration to an unsupported type.
ErrUnsupportedConfiguration = errors.New("unsupported configuration")
ErrUnsupportedConfiguration = errors.E(errors.WithCode(errors.PasswordUnsupportedConfiguration))
// ErrInvalidConfiguration results from attempting to perform an
// operation that sets a password configuration with invalid settings.
ErrInvalidConfiguration = errors.New("invalid configuration")
ErrInvalidConfiguration = errors.E(errors.WithCode(errors.PasswordInvalidConfiguration))
// ErrPasswordsEqual is returned from ChangePassword when the old and
// new passwords are equal.
ErrPasswordsEqual = errors.New("old and new password are equal")
ErrPasswordsEqual = errors.E(errors.WithCode(errors.PasswordsEqual))
)
+1 -1
View File
@@ -123,7 +123,7 @@ func (r *Repository) LookupAccount(ctx context.Context, withPublicId string, opt
a := allocAccount()
a.PublicId = withPublicId
if err := r.reader.LookupByPublicId(ctx, a); err != nil {
if errors.Is(err, errors.ErrRecordNotFound) {
if errors.IsNotFoundError(err) {
return nil, nil
}
return nil, fmt.Errorf("lookup: password account: failed %w for %s", err, withPublicId)
@@ -626,7 +626,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
masks []string
want *Account
wantCount int
wantIsErr error
wantIsErr errors.Code
}{
{
name: "nil-Account",
@@ -635,7 +635,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: makeNil(),
masks: []string{"Name", "Description"},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "nil-embedded-Account",
@@ -644,7 +644,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: makeEmbeddedNil(),
masks: []string{"Name", "Description"},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "no-public-id",
@@ -653,7 +653,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: deletePublicId(),
masks: []string{"Name", "Description"},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "updating-non-existent-Account",
@@ -664,7 +664,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: combine(nonExistentPublicId(), changeName("test-update-name-repo")),
masks: []string{"Name"},
wantIsErr: errors.ErrRecordNotFound,
wantIsErr: errors.RecordNotFound,
},
{
name: "empty-field-mask",
@@ -674,7 +674,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
},
chgFn: changeName("test-update-name-repo"),
wantIsErr: errors.ErrEmptyFieldMask,
wantIsErr: errors.EmptyFieldMask,
},
{
name: "read-only-fields-in-field-mask",
@@ -685,7 +685,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: changeName("test-update-name-repo"),
masks: []string{"PublicId", "CreateTime", "UpdateTime", "AuthMethodId"},
wantIsErr: errors.ErrInvalidFieldMask,
wantIsErr: errors.InvalidFieldMask,
},
{
name: "unknown-field-in-field-mask",
@@ -696,7 +696,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: changeName("test-update-name-repo"),
masks: []string{"Bilbo"},
wantIsErr: errors.ErrInvalidFieldMask,
wantIsErr: errors.InvalidFieldMask,
},
{
name: "change-name",
@@ -843,7 +843,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: changeLoginName("KaZmIeRcZaK"),
masks: []string{"LoginName"},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "change-login-name-to-short",
@@ -854,7 +854,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: changeLoginName("ka"),
masks: []string{"LoginName"},
wantIsErr: ErrTooShort,
wantIsErr: errors.PasswordTooShort,
},
{
name: "delete-login-name",
@@ -865,7 +865,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
},
chgFn: changeLoginName(""),
masks: []string{"LoginName"},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
}
@@ -892,8 +892,8 @@ func TestRepository_UpdateAccount(t *testing.T) {
orig = tt.chgFn(orig)
}
got, gotCount, err := repo.UpdateAccount(context.Background(), org.GetPublicId(), orig, 1, tt.masks)
if tt.wantIsErr != nil {
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Equal(tt.wantCount, gotCount, "row count")
assert.Nil(got)
return
@@ -956,7 +956,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
assert.Equal(db.NoRowsAffected, gotCount2, "row count")
err = db.TestVerifyOplog(t, rw, ab.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
assert.Error(err)
assert.True(errors.Is(errors.ErrRecordNotFound, err))
assert.True(errors.IsNotFoundError(err))
})
t.Run("valid-duplicate-names-diff-AuthMethods", func(t *testing.T) {
@@ -1034,7 +1034,7 @@ func TestRepository_UpdateAccount(t *testing.T) {
assert.Equal(db.NoRowsAffected, gotCount2, "row count")
err = db.TestVerifyOplog(t, rw, ab.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
assert.Error(err)
assert.True(errors.Is(errors.ErrRecordNotFound, err))
assert.True(errors.IsNotFoundError(err))
})
t.Run("valid-duplicate-loginnames-diff-AuthMethods", func(t *testing.T) {
@@ -104,7 +104,7 @@ func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, opt
a := allocAuthMethod()
a.PublicId = publicId
if err := r.reader.LookupByPublicId(ctx, &a); err != nil {
if errors.Is(err, errors.ErrRecordNotFound) {
if errors.IsNotFoundError(err) {
return nil, nil
}
return nil, fmt.Errorf("lookup: password auth method: failed %w for %s", err, publicId)
@@ -90,7 +90,7 @@ func (r *Repository) setArgon2Conf(ctx context.Context, scopeId string, c *Argon
func(rr db.Reader, w db.Writer) error {
where, args := c.whereDup()
if err := rr.LookupWhere(ctx, newArgon2Conf, where, args...); err != nil {
if err != errors.ErrRecordNotFound {
if !errors.IsNotFoundError(err) {
return err
}
newArgon2Conf = c.clone()
@@ -140,17 +140,17 @@ func TestRepository_GetConfiguration(t *testing.T) {
name string
authMethodId string
want *Argon2Configuration
wantIsErr error
wantIsErr errors.Code
}{
{
name: "invalid-no-authMethodId",
authMethodId: "",
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "invalid-authMethodId",
authMethodId: "abcdefghijk",
wantIsErr: errors.ErrRecordNotFound,
wantIsErr: errors.RecordNotFound,
},
{
name: "valid-authMethodId",
@@ -163,8 +163,8 @@ func TestRepository_GetConfiguration(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := repo.GetConfiguration(ctx, tt.authMethodId)
if tt.wantIsErr != nil {
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got, "returned configuration")
return
}
@@ -212,26 +212,26 @@ func TestRepository_SetConfiguration(t *testing.T) {
in Configuration
want *Argon2Configuration
wantUnknownErr bool
wantIsErr error
wantIsErr errors.Code
}{
{
name: "invalid-nil-config",
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "nil-embedded-config",
in: &Argon2Configuration{},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "invalid-no-authMethodId",
in: NewArgon2Configuration(),
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "unknown-configuration-type",
in: tconf(0),
wantIsErr: ErrUnsupportedConfiguration,
wantIsErr: errors.PasswordUnsupportedConfiguration,
},
{
name: "invalid-unknown-authMethodId",
@@ -259,7 +259,7 @@ func TestRepository_SetConfiguration(t *testing.T) {
KeyLength: 32,
},
},
wantIsErr: ErrInvalidConfiguration,
wantIsErr: errors.PasswordInvalidConfiguration,
},
{
name: "valid",
@@ -290,8 +290,8 @@ func TestRepository_SetConfiguration(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := repo.SetConfiguration(context.Background(), o.GetPublicId(), tt.in)
if tt.wantIsErr != nil {
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
if tt.wantIsErr != 0 {
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(got, "returned configuration")
return
}
@@ -306,7 +306,7 @@ func (r *Repository) SetPassword(ctx context.Context, scopeId, accountId, passwo
oldCred := allocCredential()
if err := rr.LookupWhere(ctx, &oldCred, "password_account_id = ?", accountId); err != nil {
if !errors.Is(err, errors.ErrRecordNotFound) {
if !errors.IsNotFoundError(err) {
return err
}
}
@@ -119,7 +119,7 @@ func TestRepository_Authenticate(t *testing.T) {
assert.Equal(tt.args.loginName, authAcct.LoginName, "LoginName")
err = db.TestVerifyOplog(t, rw, authAcct.CredentialId, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
assert.Error(err)
assert.True(errors.Is(errors.ErrRecordNotFound, err))
assert.True(errors.IsNotFoundError(err))
})
}
}
@@ -276,7 +276,7 @@ func TestRepository_ChangePassword(t *testing.T) {
name string
args args
wantAccount bool
wantIsErr error
wantIsErr errors.Code
}{
{
name: "invalid-no-accountId",
@@ -285,7 +285,7 @@ func TestRepository_ChangePassword(t *testing.T) {
old: passwd,
new: "12345678-changed",
},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "invalid-no-current-password",
@@ -294,7 +294,7 @@ func TestRepository_ChangePassword(t *testing.T) {
old: "",
new: "12345678-changed",
},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "invalid-no-new-password",
@@ -303,7 +303,7 @@ func TestRepository_ChangePassword(t *testing.T) {
old: passwd,
new: "",
},
wantIsErr: errors.ErrInvalidParameter,
wantIsErr: errors.InvalidParameter,
},
{
name: "invalid-same-passwords",
@@ -312,7 +312,7 @@ func TestRepository_ChangePassword(t *testing.T) {
old: passwd,
new: passwd,
},
wantIsErr: ErrPasswordsEqual,
wantIsErr: errors.PasswordsEqual,
},
{
name: "auth-failure-unknown-accountId",
@@ -322,7 +322,7 @@ func TestRepository_ChangePassword(t *testing.T) {
new: "12345678-changed",
},
wantAccount: false,
wantIsErr: errors.ErrRecordNotFound,
wantIsErr: errors.RecordNotFound,
},
{
name: "auth-failure-wrong-current-password",
@@ -332,7 +332,6 @@ func TestRepository_ChangePassword(t *testing.T) {
new: "12345678-changed",
},
wantAccount: false,
wantIsErr: nil,
},
{
name: "password-to-short",
@@ -341,7 +340,7 @@ func TestRepository_ChangePassword(t *testing.T) {
old: passwd,
new: "1",
},
wantIsErr: ErrTooShort,
wantIsErr: errors.PasswordTooShort,
},
{
name: "valid",
@@ -369,9 +368,9 @@ func TestRepository_ChangePassword(t *testing.T) {
authAcct1 := authFn(passwd, "original account")
chgAuthAcct, err := repo.ChangePassword(context.Background(), o.GetPublicId(), tt.args.acctId, tt.args.old, tt.args.new, authAcct1.Version)
if tt.wantIsErr != nil {
if tt.wantIsErr != 0 {
assert.Error(err)
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "want err: %q got: %q", tt.wantIsErr, err)
assert.Nil(chgAuthAcct, "returned account")
authAcct2 := authFn(passwd, "error changing password: using old password")
assert.Equal(authAcct1.CredentialId, authAcct2.CredentialId, "CredentialId should not change")