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

+10 -13
View File
@@ -4,26 +4,22 @@
package common
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/hashicorp/boundary/internal/errors"
"github.com/jinzhu/gorm"
)
var (
// ErrInvalidParameter is returned when a required parameter is nil.
ErrInvalidParameter = errors.New("nil parameter")
)
// UpdateFields will create a map[string]interface of the update values to be
// sent to the db. The map keys will be the field names for the fields to be
// updated. The caller provided fieldMaskPaths and setToNullPaths must not
// intersect. fieldMaskPaths and setToNullPaths cannot both be zero len.
func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []string) (map[string]interface{}, error) {
const op = "common.UpdateFields"
if i == nil {
return nil, fmt.Errorf("interface is missing: %w", ErrInvalidParameter)
return nil, errors.New(errors.InvalidParameter, op, "interface is missing")
}
if fieldMaskPaths == nil {
fieldMaskPaths = []string{}
@@ -32,7 +28,7 @@ func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []strin
setToNullPaths = []string{}
}
if len(fieldMaskPaths) == 0 && len(setToNullPaths) == 0 {
return nil, errors.New("both fieldMaskPaths and setToNullPaths are zero len")
return nil, errors.New(errors.InvalidParameter, op, "both fieldMaskPaths and setToNullPaths are zero len")
}
inter, maskPaths, nullPaths, err := Intersection(fieldMaskPaths, setToNullPaths)
@@ -40,7 +36,7 @@ func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []strin
return nil, err
}
if len(inter) != 0 {
return nil, fmt.Errorf("fieldMashPaths and setToNullPaths cannot intersect")
return nil, errors.New(errors.InvalidParameter, op, "fieldMashPaths and setToNullPaths cannot intersect")
}
updateFields := map[string]interface{}{} // case sensitive update fields to values
@@ -91,11 +87,11 @@ func UpdateFields(i interface{}, fieldMaskPaths []string, setToNullPaths []strin
}
if missing := findMissingPaths(setToNullPaths, found); len(missing) != 0 {
return nil, fmt.Errorf("null paths not found in resource: %s", missing)
return nil, errors.New(errors.InvalidParameter, op, fmt.Sprintf("null paths not found in resource: %s", missing))
}
if missing := findMissingPaths(fieldMaskPaths, found); len(missing) != 0 {
return nil, fmt.Errorf("field mask paths not found in resource: %s", missing)
return nil, errors.New(errors.InvalidParameter, op, fmt.Sprintf("field mask paths not found in resource: %s", missing))
}
return updateFields, nil
@@ -116,11 +112,12 @@ func findMissingPaths(paths []string, foundPaths map[string]struct{}) []string {
// of the original av and bv, with the key set to uppercase and value set to the
// original
func Intersection(av, bv []string) ([]string, map[string]string, map[string]string, error) {
const op = "common.Intersection"
if av == nil {
return nil, nil, nil, fmt.Errorf("av is missing: %w", ErrInvalidParameter)
return nil, nil, nil, errors.New(errors.InvalidParameter, op, "av is missing")
}
if bv == nil {
return nil, nil, nil, fmt.Errorf("bv is missing: %w", ErrInvalidParameter)
return nil, nil, nil, errors.New(errors.InvalidParameter, op, "bv is missing")
}
if len(av) == 0 && len(bv) == 0 {
return []string{}, map[string]string{}, map[string]string{}, nil
+9 -9
View File
@@ -147,7 +147,7 @@ func Test_intersection(t *testing.T) {
want1: nil,
want2: nil,
wantErr: true,
wantErrMsg: "av is missing: nil parameter",
wantErrMsg: "common.Intersection: av is missing: parameter violation: error #100",
},
{
name: "nil-bv",
@@ -159,7 +159,7 @@ func Test_intersection(t *testing.T) {
want1: nil,
want2: nil,
wantErr: true,
wantErrMsg: "bv is missing: nil parameter",
wantErrMsg: "common.Intersection: bv is missing: parameter violation: error #100",
},
}
for _, tt := range tests {
@@ -206,7 +206,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "interface is missing: nil parameter",
wantErrMsg: "common.UpdateFields: interface is missing: parameter violation: error #100",
},
{
name: "missing fieldmasks",
@@ -217,7 +217,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "both fieldMaskPaths and setToNullPaths are zero len",
wantErrMsg: "common.UpdateFields: both fieldMaskPaths and setToNullPaths are zero len: parameter violation: error #100",
},
{
name: "missing null fields",
@@ -239,7 +239,7 @@ func TestUpdateFields(t *testing.T) {
setToNullPaths: nil,
},
wantErr: true,
wantErrMsg: "both fieldMaskPaths and setToNullPaths are zero len",
wantErrMsg: "common.UpdateFields: both fieldMaskPaths and setToNullPaths are zero len: parameter violation: error #100",
},
{
name: "not found masks",
@@ -250,7 +250,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "field mask paths not found in resource: [invalidFieldName]",
wantErrMsg: "common.UpdateFields: field mask paths not found in resource: [invalidFieldName]: parameter violation: error #100",
},
{
name: "not found null paths",
@@ -261,7 +261,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "null paths not found in resource: [invalidFieldName]",
wantErrMsg: "common.UpdateFields: null paths not found in resource: [invalidFieldName]: parameter violation: error #100",
},
{
name: "intersection",
@@ -272,7 +272,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "fieldMashPaths and setToNullPaths cannot intersect",
wantErrMsg: "common.UpdateFields: fieldMashPaths and setToNullPaths cannot intersect: parameter violation: error #100",
},
{
name: "valid",
@@ -381,7 +381,7 @@ func TestUpdateFields(t *testing.T) {
},
want: nil,
wantErr: true,
wantErrMsg: "null paths not found in resource: [invalidFieldName]",
wantErrMsg: "common.UpdateFields: null paths not found in resource: [invalidFieldName]: parameter violation: error #100",
},
}
for _, tt := range tests {