From d64d1fbb3d937850e82b4d1268c8ce57f28de83d Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 3 Nov 2015 13:35:11 -0800 Subject: [PATCH] s/ValidationError/Error/ --- pkg/api/errors/errors.go | 2 +- pkg/api/errors/errors_test.go | 2 +- pkg/api/validation/validation.go | 2 +- pkg/api/validation/validation_test.go | 38 +++++------ .../extensions/validation/validation_test.go | 8 +-- pkg/util/validation/errors.go | 66 +++++++++---------- pkg/util/validation/errors_test.go | 22 +++---- 7 files changed, 70 insertions(+), 70 deletions(-) diff --git a/pkg/api/errors/errors.go b/pkg/api/errors/errors.go index 4ad72721f93..0e0edf6fae2 100644 --- a/pkg/api/errors/errors.go +++ b/pkg/api/errors/errors.go @@ -162,7 +162,7 @@ func NewConflict(kind, name string, err error) error { func NewInvalid(kind, name string, errs validation.ValidationErrorList) error { causes := make([]unversioned.StatusCause, 0, len(errs)) for i := range errs { - if err, ok := errs[i].(*validation.ValidationError); ok { + if err, ok := errs[i].(*validation.Error); ok { causes = append(causes, unversioned.StatusCause{ Type: unversioned.CauseType(err.Type), Message: err.ErrorBody(), diff --git a/pkg/api/errors/errors_test.go b/pkg/api/errors/errors_test.go index 138311cbe44..0f24dcb26aa 100644 --- a/pkg/api/errors/errors_test.go +++ b/pkg/api/errors/errors_test.go @@ -88,7 +88,7 @@ func TestErrorNew(t *testing.T) { func TestNewInvalid(t *testing.T) { testCases := []struct { - Err *validation.ValidationError + Err *validation.Error Details *unversioned.StatusDetails }{ { diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 5bd0f61b3b8..ca67d2a3bd9 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -1497,7 +1497,7 @@ func ValidateNodeUpdate(node, oldNode *api.Node) validation.ValidationErrorList // Clear status oldNode.Status = node.Status - // TODO: Add a 'real' ValidationError type for this error and provide print actual diffs. + // TODO: Add a 'real' error type for this error and provide print actual diffs. if !api.Semantic.DeepEqual(oldNode, node) { glog.V(4).Infof("Update failed validation %#v vs %#v", oldNode, node) allErrs = append(allErrs, fmt.Errorf("update contains more than labels or capacity changes")) diff --git a/pkg/api/validation/validation_test.go b/pkg/api/validation/validation_test.go index c7788135aa4..345b04723b5 100644 --- a/pkg/api/validation/validation_test.go +++ b/pkg/api/validation/validation_test.go @@ -36,7 +36,7 @@ import ( func expectPrefix(t *testing.T, prefix string, errs validation.ValidationErrorList) { for i := range errs { - if f, p := errs[i].(*validation.ValidationError).Field, prefix; !strings.HasPrefix(f, p) { + if f, p := errs[i].(*validation.Error).Field, prefix; !strings.HasPrefix(f, p) { t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i]) } } @@ -150,7 +150,7 @@ func TestValidateLabels(t *testing.T) { if len(errs) != 1 { t.Errorf("case[%d] expected failure", i) } else { - detail := errs[0].(*validation.ValidationError).Detail + detail := errs[0].(*validation.Error).Detail if detail != qualifiedNameErrorMsg { t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg) } @@ -168,7 +168,7 @@ func TestValidateLabels(t *testing.T) { if len(errs) != 1 { t.Errorf("case[%d] expected failure", i) } else { - detail := errs[0].(*validation.ValidationError).Detail + detail := errs[0].(*validation.Error).Detail if detail != labelValueErrorMsg { t.Errorf("error detail %s should be equal %s", detail, labelValueErrorMsg) } @@ -215,7 +215,7 @@ func TestValidateAnnotations(t *testing.T) { if len(errs) != 1 { t.Errorf("case[%d] expected failure", i) } - detail := errs[0].(*validation.ValidationError).Detail + detail := errs[0].(*validation.Error).Detail if detail != qualifiedNameErrorMsg { t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg) } @@ -568,13 +568,13 @@ func TestValidateVolumes(t *testing.T) { continue } for i := range errs { - if errs[i].(*validation.ValidationError).Type != v.T { + if errs[i].(*validation.Error).Type != v.T { t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i]) } - if errs[i].(*validation.ValidationError).Field != v.F { + if errs[i].(*validation.Error).Field != v.F { t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i]) } - detail := errs[i].(*validation.ValidationError).Detail + detail := errs[i].(*validation.Error).Detail if detail != v.D { t.Errorf("%s: expected error detail \"%s\", got \"%s\"", k, v.D, detail) } @@ -627,13 +627,13 @@ func TestValidatePorts(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - if errs[i].(*validation.ValidationError).Type != v.T { + if errs[i].(*validation.Error).Type != v.T { t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i]) } - if errs[i].(*validation.ValidationError).Field != v.F { + if errs[i].(*validation.Error).Field != v.F { t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i]) } - detail := errs[i].(*validation.ValidationError).Detail + detail := errs[i].(*validation.Error).Detail if detail != v.D { t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail) } @@ -772,7 +772,7 @@ func TestValidateEnv(t *testing.T) { t.Errorf("expected failure for %s", tc.name) } else { for i := range errs { - str := errs[i].(*validation.ValidationError).Error() + str := errs[i].(*validation.Error).Error() if str != "" && str != tc.expectedError { t.Errorf("%s: expected error detail either empty or %s, got %s", tc.name, tc.expectedError, str) } @@ -2560,7 +2560,7 @@ func TestValidateReplicationController(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - field := errs[i].(*validation.ValidationError).Field + field := errs[i].(*validation.Error).Field if !strings.HasPrefix(field, "spec.template.") && field != "metadata.name" && field != "metadata.namespace" && @@ -2676,7 +2676,7 @@ func TestValidateNode(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - field := errs[i].(*validation.ValidationError).Field + field := errs[i].(*validation.Error).Field expectedFields := map[string]bool{ "metadata.name": true, "metadata.labels": true, @@ -3008,7 +3008,7 @@ func TestValidateResourceNames(t *testing.T) { } else if len(err) == 0 && !item.success { t.Errorf("expected failure for input %q", item.input) for i := range err { - detail := err[i].(*validation.ValidationError).Detail + detail := err[i].(*validation.Error).Detail if detail != "" && detail != qualifiedNameErrorMsg { t.Errorf("%d: expected error detail either empty or %s, got %s", k, qualifiedNameErrorMsg, detail) } @@ -3224,7 +3224,7 @@ func TestValidateLimitRange(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - detail := errs[i].(*validation.ValidationError).Detail + detail := errs[i].(*validation.Error).Detail if detail != v.D { t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail) } @@ -3329,8 +3329,8 @@ func TestValidateResourceQuota(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - field := errs[i].(*validation.ValidationError).Field - detail := errs[i].(*validation.ValidationError).Detail + field := errs[i].(*validation.Error).Field + detail := errs[i].(*validation.Error).Detail if field != "metadata.name" && field != "metadata.namespace" && !api.IsStandardResourceName(field) { t.Errorf("%s: missing prefix for: %v", k, field) } @@ -3937,7 +3937,7 @@ func TestValidateEndpoints(t *testing.T) { } for k, v := range errorCases { - if errs := ValidateEndpoints(&v.endpoints); len(errs) == 0 || errs[0].(*validation.ValidationError).Type != v.errorType || !strings.Contains(errs[0].(*validation.ValidationError).Detail, v.errorDetail) { + if errs := ValidateEndpoints(&v.endpoints); len(errs) == 0 || errs[0].(*validation.Error).Type != v.errorType || !strings.Contains(errs[0].(*validation.Error).Detail, v.errorDetail) { t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs) } } @@ -4017,7 +4017,7 @@ func TestValidateSecurityContext(t *testing.T) { }, } for k, v := range errorCases { - if errs := ValidateSecurityContext(v.sc); len(errs) == 0 || errs[0].(*validation.ValidationError).Type != v.errorType || errs[0].(*validation.ValidationError).Detail != v.errorDetail { + if errs := ValidateSecurityContext(v.sc); len(errs) == 0 || errs[0].(*validation.Error).Type != v.errorType || errs[0].(*validation.Error).Detail != v.errorDetail { t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs) } } diff --git a/pkg/apis/extensions/validation/validation_test.go b/pkg/apis/extensions/validation/validation_test.go index f5e70ebcba6..94d74de2fe7 100644 --- a/pkg/apis/extensions/validation/validation_test.go +++ b/pkg/apis/extensions/validation/validation_test.go @@ -675,7 +675,7 @@ func TestValidateDaemonSet(t *testing.T) { t.Errorf("expected failure for %s", k) } for i := range errs { - field := errs[i].(*validation.ValidationError).Field + field := errs[i].(*validation.Error).Field if !strings.HasPrefix(field, "spec.template.") && field != "metadata.name" && field != "metadata.namespace" && @@ -918,7 +918,7 @@ func TestValidateJob(t *testing.T) { t.Errorf("expected failure for %s", k) } else { s := strings.Split(k, ":") - err := errs[0].(*validation.ValidationError) + err := errs[0].(*validation.Error) if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) { t.Errorf("unexpected error: %v, expected: %s", errs[0], k) } @@ -1019,7 +1019,7 @@ func TestValidateIngress(t *testing.T) { t.Errorf("expected failure for %s", k) } else { s := strings.Split(k, ":") - err := errs[0].(*validation.ValidationError) + err := errs[0].(*validation.Error) if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) { t.Errorf("unexpected error: %v, expected: %s", errs[0], k) } @@ -1111,7 +1111,7 @@ func TestValidateIngressStatusUpdate(t *testing.T) { t.Errorf("expected failure for %s", k) } else { s := strings.Split(k, ":") - err := errs[0].(*validation.ValidationError) + err := errs[0].(*validation.Error) if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) { t.Errorf("unexpected error: %v, expected: %s", errs[0], k) } diff --git a/pkg/util/validation/errors.go b/pkg/util/validation/errors.go index 43473fc7d56..37b90e380b9 100644 --- a/pkg/util/validation/errors.go +++ b/pkg/util/validation/errors.go @@ -78,21 +78,21 @@ func (t ErrorType) String() string { } } -// ValidationError is an implementation of the 'error' interface, which represents an error of validation. -type ValidationError struct { +// Error is an implementation of the 'error' interface, which represents an error of validation. +type Error struct { Type ErrorType Field string BadValue interface{} Detail string } -var _ error = &ValidationError{} +var _ error = &Error{} -func (v *ValidationError) Error() string { +func (v *Error) Error() string { return fmt.Sprintf("%s: %s", v.Field, v.ErrorBody()) } -func (v *ValidationError) ErrorBody() string { +func (v *Error) ErrorBody() string { var s string switch v.Type { case ErrorTypeRequired, ErrorTypeTooLong: @@ -106,51 +106,51 @@ func (v *ValidationError) ErrorBody() string { return s } -// NewFieldRequired returns a *ValidationError indicating "value required" -func NewFieldRequired(field string) *ValidationError { - return &ValidationError{ErrorTypeRequired, field, "", ""} +// NewFieldRequired returns a *Error indicating "value required" +func NewFieldRequired(field string) *Error { + return &Error{ErrorTypeRequired, field, "", ""} } -// NewFieldInvalid returns a *ValidationError indicating "invalid value" -func NewFieldInvalid(field string, value interface{}, detail string) *ValidationError { - return &ValidationError{ErrorTypeInvalid, field, value, detail} +// NewFieldInvalid returns a *Error indicating "invalid value" +func NewFieldInvalid(field string, value interface{}, detail string) *Error { + return &Error{ErrorTypeInvalid, field, value, detail} } -// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value" -func NewFieldValueNotSupported(field string, value interface{}, validValues []string) *ValidationError { +// NewFieldValueNotSupported returns a *Error indicating "unsupported value" +func NewFieldValueNotSupported(field string, value interface{}, validValues []string) *Error { detail := "" if validValues != nil && len(validValues) > 0 { detail = "supported values: " + strings.Join(validValues, ", ") } - return &ValidationError{ErrorTypeNotSupported, field, value, detail} + return &Error{ErrorTypeNotSupported, field, value, detail} } -// NewFieldForbidden returns a *ValidationError indicating "forbidden" -func NewFieldForbidden(field string, value interface{}) *ValidationError { - return &ValidationError{ErrorTypeForbidden, field, value, ""} +// NewFieldForbidden returns a *Error indicating "forbidden" +func NewFieldForbidden(field string, value interface{}) *Error { + return &Error{ErrorTypeForbidden, field, value, ""} } -// NewFieldDuplicate returns a *ValidationError indicating "duplicate value" -func NewFieldDuplicate(field string, value interface{}) *ValidationError { - return &ValidationError{ErrorTypeDuplicate, field, value, ""} +// NewFieldDuplicate returns a *Error indicating "duplicate value" +func NewFieldDuplicate(field string, value interface{}) *Error { + return &Error{ErrorTypeDuplicate, field, value, ""} } -// NewFieldNotFound returns a *ValidationError indicating "value not found" -func NewFieldNotFound(field string, value interface{}) *ValidationError { - return &ValidationError{ErrorTypeNotFound, field, value, ""} +// NewFieldNotFound returns a *Error indicating "value not found" +func NewFieldNotFound(field string, value interface{}) *Error { + return &Error{ErrorTypeNotFound, field, value, ""} } -func NewFieldTooLong(field string, value interface{}, maxLength int) *ValidationError { - return &ValidationError{ErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)} +func NewFieldTooLong(field string, value interface{}, maxLength int) *Error { + return &Error{ErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)} } type ValidationErrorList []error -// Prefix adds a prefix to the Field of every ValidationError in the list. +// Prefix adds a prefix to the Field of every Error in the list. // Returns the list for convenience. func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList { for i := range list { - if err, ok := list[i].(*ValidationError); ok { + if err, ok := list[i].(*Error); ok { if strings.HasPrefix(err.Field, "[") { err.Field = prefix + err.Field } else if len(err.Field) != 0 { @@ -160,23 +160,23 @@ func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList { } list[i] = err } else { - panic(fmt.Sprintf("Programmer error: ValidationErrorList holds non-ValidationError: %#v", list[i])) + panic(fmt.Sprintf("Programmer error: ValidationErrorList holds non-Error: %#v", list[i])) } } return list } -// PrefixIndex adds an index to the Field of every ValidationError in the list. +// PrefixIndex adds an index to the Field of every Error in the list. // Returns the list for convenience. func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList { return list.Prefix(fmt.Sprintf("[%d]", index)) } // NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true -// if the provided error is a ValidationError and has the provided ErrorType. +// if the provided error is a Error and has the provided ErrorType. func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher { return func(err error) bool { - if e, ok := err.(*ValidationError); ok { + if e, ok := err.(*Error); ok { return e.Type == t } return false @@ -184,11 +184,11 @@ func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher { } // NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true -// if the provided error is a ValidationError and has a field with the provided +// if the provided error is a Error and has a field with the provided // prefix. func NewValidationErrorFieldPrefixMatcher(prefix string) utilerrors.Matcher { return func(err error) bool { - if e, ok := err.(*ValidationError); ok { + if e, ok := err.(*Error); ok { return strings.HasPrefix(e.Field, prefix) } return false diff --git a/pkg/util/validation/errors_test.go b/pkg/util/validation/errors_test.go index a73e3af1260..7c74f13a6ef 100644 --- a/pkg/util/validation/errors_test.go +++ b/pkg/util/validation/errors_test.go @@ -23,27 +23,27 @@ import ( func TestMakeFuncs(t *testing.T) { testCases := []struct { - fn func() *ValidationError + fn func() *Error expected ErrorType }{ { - func() *ValidationError { return NewFieldInvalid("f", "v", "d") }, + func() *Error { return NewFieldInvalid("f", "v", "d") }, ErrorTypeInvalid, }, { - func() *ValidationError { return NewFieldValueNotSupported("f", "v", nil) }, + func() *Error { return NewFieldValueNotSupported("f", "v", nil) }, ErrorTypeNotSupported, }, { - func() *ValidationError { return NewFieldDuplicate("f", "v") }, + func() *Error { return NewFieldDuplicate("f", "v") }, ErrorTypeDuplicate, }, { - func() *ValidationError { return NewFieldNotFound("f", "v") }, + func() *Error { return NewFieldNotFound("f", "v") }, ErrorTypeNotFound, }, { - func() *ValidationError { return NewFieldRequired("f") }, + func() *Error { return NewFieldRequired("f") }, ErrorTypeRequired, }, } @@ -56,7 +56,7 @@ func TestMakeFuncs(t *testing.T) { } } -func TestValidationErrorUsefulMessage(t *testing.T) { +func TestErrorUsefulMessage(t *testing.T) { s := NewFieldInvalid("foo", "bar", "deet").Error() t.Logf("message: %v", s) for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} { @@ -121,7 +121,7 @@ func TestErrListFilter(t *testing.T) { func TestErrListPrefix(t *testing.T) { testCases := []struct { - Err *ValidationError + Err *Error Expected string }{ { @@ -143,7 +143,7 @@ func TestErrListPrefix(t *testing.T) { if prefix == nil || len(prefix) != len(errList) { t.Errorf("Prefix should return self") } - if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a { + if e, a := testCase.Expected, errList[0].(*Error).Field; e != a { t.Errorf("expected %s, got %s", e, a) } } @@ -151,7 +151,7 @@ func TestErrListPrefix(t *testing.T) { func TestErrListPrefixIndex(t *testing.T) { testCases := []struct { - Err *ValidationError + Err *Error Expected string }{ { @@ -173,7 +173,7 @@ func TestErrListPrefixIndex(t *testing.T) { if prefix == nil || len(prefix) != len(errList) { t.Errorf("PrefixIndex should return self") } - if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a { + if e, a := testCase.Expected, errList[0].(*Error).Field; e != a { t.Errorf("expected %s, got %s", e, a) } }