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

+18 -1
View File
@@ -27,6 +27,21 @@ const (
InvalidFieldMask Code = 103 // InvalidFieldMask represents an invalid field mast for an operation
EmptyFieldMask Code = 104 // EmptyFieldMask represents an empty field mask for an operation
// PasswordTooShort results from attempting to set a password which is to short.
PasswordTooShort Code = 200
// PasswordUnsupportedConfiguration results from attempting to perform an
// operation that sets a password configuration to an unsupported type.
PasswordUnsupportedConfiguration Code = 201
// PasswordInvalidConfiguration results from attempting to perform an
// operation that sets a valid password configuration with invalid settings.
PasswordInvalidConfiguration Code = 202
// PasswordsEqual is returned from ChangePassword when the old and
// new passwords are equal.
PasswordsEqual Code = 203
// DB errors are reserved Codes from 1000-1999
CheckConstraint Code = 1000 // CheckConstraint represents a check constraint error
NotNull Code = 1001 // NotNull represents a value must not be null error
@@ -35,5 +50,7 @@ const (
MissingTable Code = 1004 // Missing table represents an undefined table error
RecordNotFound Code = 1100 // RecordNotFound represents that a record/row was not found matching the criteria
MultipleRecords Code = 1101 // MultipleRecords represents that multiple records/rows were found matching the criteria
ColumnNotFound Code = 1102 // ColumnNotFound represent that a column was not found in the underlying db
MaxRetries Code = 1103 // MaxRetries represent that a db Tx hit max retires allowed
Exception Code = 1104 // Exception represent that an underlying db exception was raised
)
+35
View File
@@ -52,6 +52,26 @@ func TestCode_Both_String_Info(t *testing.T) {
c: EmptyFieldMask,
want: EmptyFieldMask,
},
{
name: "PasswordTooShort",
c: PasswordTooShort,
want: PasswordTooShort,
},
{
name: "PasswordUnsupportedConfiguration",
c: PasswordUnsupportedConfiguration,
want: PasswordUnsupportedConfiguration,
},
{
name: "PasswordInvalidConfiguration",
c: PasswordInvalidConfiguration,
want: PasswordInvalidConfiguration,
},
{
name: "PasswordsEqual",
c: PasswordsEqual,
want: PasswordsEqual,
},
{
name: "CheckConstraint",
c: CheckConstraint,
@@ -72,6 +92,21 @@ func TestCode_Both_String_Info(t *testing.T) {
c: RecordNotFound,
want: RecordNotFound,
},
{
name: "ColumnNotFound",
c: ColumnNotFound,
want: ColumnNotFound,
},
{
name: "MaxRetries",
c: MaxRetries,
want: MaxRetries,
},
{
name: "Exception",
c: Exception,
want: Exception,
},
{
name: "MultipleRecords",
c: MultipleRecords,
+11 -4
View File
@@ -117,8 +117,10 @@ func Convert(e error) *Err {
if e == nil {
return nil
}
var err *Err
if As(e, &err) {
// TODO instead of casting the error here, we should do an As.
// Currently doing an As loses any additional context added by non-refactored packages
// that are still wrapping with stdlib
if err, ok := e.(*Err); ok {
return err
}
var pqError *pq.Error
@@ -126,7 +128,7 @@ func Convert(e error) *Err {
if pqError.Code.Class() == "23" { // class of integrity constraint violations
switch pqError.Code {
case "23505": // unique_violation
return E(WithMsg(pqError.Detail), WithWrap(ErrNotUnique)).(*Err)
return E(WithMsg(pqError.Message), WithWrap(ErrNotUnique)).(*Err)
case "23502": // not_null_violation
msg := fmt.Sprintf("%s must not be empty", pqError.Column)
return E(WithMsg(msg), WithWrap(ErrNotNull)).(*Err)
@@ -137,8 +139,13 @@ func Convert(e error) *Err {
return E(WithCode(NotSpecificIntegrity), WithMsg(pqError.Message)).(*Err)
}
}
if pqError.Code == "42P01" {
switch pqError.Code {
case "42P01":
return E(WithCode(MissingTable), WithMsg(pqError.Message)).(*Err)
case "42703":
return E(WithCode(ColumnNotFound), WithMsg(pqError.Message)).(*Err)
case "P0001":
return E(WithCode(Exception), WithMsg(pqError.Message)).(*Err)
}
}
// unfortunately, we can't help.
+4 -4
View File
@@ -451,7 +451,7 @@ func TestConvertError(t *testing.T) {
e := errors.Convert(err)
require.NotNil(e)
assert.True(errors.Is(e, errors.ErrNotUnique))
assert.Equal("Key (name)=(alice) already exists.: unique constraint violation: integrity violation: error #1002", e.Error())
assert.Equal("db.Exec: duplicate key value violates unique constraint \"test_table_name_key\": unique constraint violation: integrity violation: error #1002", e.Error())
})
t.Run("ErrCodeNotNull", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@@ -463,7 +463,7 @@ func TestConvertError(t *testing.T) {
e := errors.Convert(err)
require.NotNil(e)
assert.True(errors.Is(e, errors.ErrNotNull))
assert.Equal("description must not be empty: not null constraint violated: integrity violation: error #1001", e.Error())
assert.Equal("db.Exec: description must not be empty: not null constraint violated: integrity violation: error #1001", e.Error())
})
t.Run("ErrCodeCheckConstraint", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@@ -475,7 +475,7 @@ func TestConvertError(t *testing.T) {
e := errors.Convert(err)
require.NotNil(e)
assert.True(errors.Is(e, errors.ErrCheckConstraint))
assert.Equal("test_table_five_check constraint failed: check constraint violated: integrity violation: error #1000", e.Error())
assert.Equal("db.Exec: test_table_five_check constraint failed: check constraint violated: integrity violation: error #1000", e.Error())
})
t.Run("MissingTable", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@@ -485,6 +485,6 @@ func TestConvertError(t *testing.T) {
e := errors.Convert(err)
require.NotNil(e)
assert.True(errors.Match(errors.T(errors.MissingTable), e))
assert.Equal("relation \"not_a_defined_table\" does not exist: integrity violation: error #1004", e.Error())
assert.Equal("db.Exec: relation \"not_a_defined_table\" does not exist: integrity violation: error #1004", e.Error())
})
}
+28
View File
@@ -36,6 +36,22 @@ var errorCodeInfo = map[Code]Info{
Message: "empty field mask",
Kind: Parameter,
},
PasswordTooShort: {
Message: "too short",
Kind: Password,
},
PasswordUnsupportedConfiguration: {
Message: "unable to support the password config type",
Kind: Password,
},
PasswordInvalidConfiguration: {
Message: "invalid parameters in password configuration",
Kind: Password,
},
PasswordsEqual: {
Message: "old and new password are equal",
Kind: Password,
},
CheckConstraint: {
Message: "constraint check failed",
Kind: Integrity,
@@ -56,6 +72,10 @@ var errorCodeInfo = map[Code]Info{
Message: "missing table",
Kind: Integrity,
},
ColumnNotFound: {
Message: "column not found",
Kind: Integrity,
},
RecordNotFound: {
Message: "record not found",
Kind: Search,
@@ -64,4 +84,12 @@ var errorCodeInfo = map[Code]Info{
Message: "multiple records",
Kind: Search,
},
Exception: {
Message: "db exception",
Kind: Integrity,
},
MaxRetries: {
Message: "too many retries",
Kind: Transaction,
},
}
+8 -20
View File
@@ -13,11 +13,8 @@ func IsUniqueError(err error) bool {
return false
}
var domainErr *Err
if errors.As(err, &domainErr) {
if domainErr.Code == NotUnique {
return true
}
if Match(T(NotUnique), err) {
return true
}
var pqError *pq.Error
@@ -37,11 +34,8 @@ func IsCheckConstraintError(err error) bool {
return false
}
var domainErr *Err
if errors.As(err, &domainErr) {
if domainErr.Code == CheckConstraint {
return true
}
if Match(T(CheckConstraint), err) {
return true
}
var pqError *pq.Error
@@ -61,11 +55,8 @@ func IsNotNullError(err error) bool {
return false
}
var domainErr *Err
if errors.As(err, &domainErr) {
if domainErr.Code == NotNull {
return true
}
if Match(T(NotNull), err) {
return true
}
var pqError *pq.Error
@@ -97,11 +88,8 @@ func IsNotFoundError(err error) bool {
return false
}
var domainErr *Err
if errors.As(err, &domainErr) {
if domainErr.Code == RecordNotFound {
return true
}
if Match(T(RecordNotFound), err) {
return true
}
return false
+8 -4
View File
@@ -8,13 +8,17 @@ const (
Parameter
Integrity
Search
Password
Transaction
)
func (e Kind) String() string {
return map[Kind]string{
Other: "unknown",
Parameter: "parameter violation",
Integrity: "integrity violation",
Search: "search issue",
Other: "unknown",
Parameter: "parameter violation",
Integrity: "integrity violation",
Search: "search issue",
Password: "password violation",
Transaction: "db transaction issue",
}[e]
}
+4 -4
View File
@@ -60,15 +60,15 @@ func (t *Template) Error() string {
return "Template error"
}
// Match the template against the error. The error must be a *Err, or match
// will return false. Matches all non-empty fields of the template against the
// Match the template against the error. The error must be of type *Err, or wrap an error of type *Err,
// otherwise match will return false. Matches all non-empty fields of the template against the
// error.
func Match(t *Template, err error) bool {
if t == nil || err == nil {
return false
}
e, ok := err.(*Err)
if !ok {
var e *Err
if !As(err, &e) {
return false
}
+2 -2
View File
@@ -14,11 +14,11 @@ var (
// ErrInvalidFieldMask is returned by update methods if the field mask
// contains unknown fields or fields that cannot be updated.
ErrInvalidFieldMask = E(WithCode(InvalidParameter), WithMsg("invalid field mask"))
ErrInvalidFieldMask = E(WithCode(InvalidFieldMask), WithMsg("invalid field mask"))
// ErrEmptyFieldMask is returned by update methods if the field mask is
// empty.
ErrEmptyFieldMask = E(WithCode(InvalidParameter), WithMsg("empty field mask"))
ErrEmptyFieldMask = E(WithCode(EmptyFieldMask), WithMsg("empty field mask"))
// ErrNotUnique is returned by create and update methods when a write
// to the repository resulted in a unique constraint violation.