Change ValidationError to a pointer

In Prep for adding at leats one more field, make this operate more list
StatusError.
This commit is contained in:
Tim Hockin
2014-11-21 06:11:23 +08:00
parent d4c37e352e
commit b08e5b24b0
7 changed files with 59 additions and 60 deletions

View File

@@ -30,6 +30,8 @@ type StatusError struct {
ErrStatus api.Status
}
var _ error = &statusError{}
// Error implements the Error interface.
func (e *StatusError) Error() string {
return e.ErrStatus.Message
@@ -107,7 +109,7 @@ func NewConflict(kind, name string, err error) error {
func NewInvalid(kind, name string, errs ValidationErrorList) error {
causes := make([]api.StatusCause, 0, len(errs))
for i := range errs {
if err, ok := errs[i].(ValidationError); ok {
if err, ok := errs[i].(*ValidationError); ok {
causes = append(causes, api.StatusCause{
Type: api.CauseType(err.Type),
Message: err.Error(),
@@ -130,18 +132,16 @@ func NewInvalid(kind, name string, errs ValidationErrorList) error {
// NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
func NewBadRequest(reason string) error {
return &StatusError{
api.Status{
Status: api.StatusFailure,
Code: http.StatusBadRequest,
Reason: api.StatusReasonBadRequest,
Details: &api.StatusDetails{
Causes: []api.StatusCause{
{Message: reason},
},
return &StatusError{api.Status{
Status: api.StatusFailure,
Code: http.StatusBadRequest,
Reason: api.StatusReasonBadRequest,
Details: &api.StatusDetails{
Causes: []api.StatusCause{
{Message: reason},
},
},
}
}}
}
// NewInternalError returns an error indicating the item is invalid and cannot be processed.

View File

@@ -54,7 +54,7 @@ func TestErrorNew(t *testing.T) {
func TestNewInvalid(t *testing.T) {
testCases := []struct {
Err ValidationError
Err *ValidationError
Details *api.StatusDetails
}{
{

View File

@@ -80,38 +80,40 @@ type ValidationError struct {
BadValue interface{}
}
func (v ValidationError) Error() string {
var _ error = &ValidationError{}
func (v *ValidationError) Error() string {
return fmt.Sprintf("%s: %v '%v'", v.Field, ValueOf(v.Type), v.BadValue)
}
// NewFieldRequired returns a ValidationError indicating "value required"
func NewFieldRequired(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeRequired, field, value}
// NewFieldRequired returns a *ValidationError indicating "value required"
func NewFieldRequired(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeRequired, field, value}
}
// NewFieldInvalid returns a ValidationError indicating "invalid value"
func NewFieldInvalid(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeInvalid, field, value}
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
func NewFieldInvalid(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeInvalid, field, value}
}
// NewFieldNotSupported returns a ValidationError indicating "unsupported value"
func NewFieldNotSupported(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeNotSupported, field, value}
// NewFieldNotSupported returns a *ValidationError indicating "unsupported value"
func NewFieldNotSupported(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeNotSupported, field, value}
}
// NewFieldForbidden returns a ValidationError indicating "forbidden"
func NewFieldForbidden(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeForbidden, field, value}
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
func NewFieldForbidden(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeForbidden, field, value}
}
// NewFieldDuplicate returns a ValidationError indicating "duplicate value"
func NewFieldDuplicate(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeDuplicate, field, value}
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
func NewFieldDuplicate(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeDuplicate, field, value}
}
// NewFieldNotFound returns a ValidationError indicating "value not found"
func NewFieldNotFound(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeNotFound, field, value}
// NewFieldNotFound returns a *ValidationError indicating "value not found"
func NewFieldNotFound(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeNotFound, field, value}
}
// ValidationErrorList is a collection of ValidationErrors. This does not
@@ -131,7 +133,7 @@ func (list ValidationErrorList) ToError() error {
// 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].(*ValidationError); ok {
if strings.HasPrefix(err.Field, "[") {
err.Field = prefix + err.Field
} else if len(err.Field) != 0 {

View File

@@ -23,27 +23,27 @@ import (
func TestMakeFuncs(t *testing.T) {
testCases := []struct {
fn func() ValidationError
fn func() *ValidationError
expected ValidationErrorType
}{
{
func() ValidationError { return NewFieldInvalid("f", "v") },
func() *ValidationError { return NewFieldInvalid("f", "v") },
ValidationErrorTypeInvalid,
},
{
func() ValidationError { return NewFieldNotSupported("f", "v") },
func() *ValidationError { return NewFieldNotSupported("f", "v") },
ValidationErrorTypeNotSupported,
},
{
func() ValidationError { return NewFieldDuplicate("f", "v") },
func() *ValidationError { return NewFieldDuplicate("f", "v") },
ValidationErrorTypeDuplicate,
},
{
func() ValidationError { return NewFieldNotFound("f", "v") },
func() *ValidationError { return NewFieldNotFound("f", "v") },
ValidationErrorTypeNotFound,
},
{
func() ValidationError { return NewFieldRequired("f", "v") },
func() *ValidationError { return NewFieldRequired("f", "v") },
ValidationErrorTypeRequired,
},
}
@@ -65,7 +65,7 @@ func TestValidationError(t *testing.T) {
func TestErrListPrefix(t *testing.T) {
testCases := []struct {
Err ValidationError
Err *ValidationError
Expected string
}{
{
@@ -87,7 +87,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].(*ValidationError).Field; e != a {
t.Errorf("expected %s, got %s", e, a)
}
}
@@ -95,7 +95,7 @@ func TestErrListPrefix(t *testing.T) {
func TestErrListPrefixIndex(t *testing.T) {
testCases := []struct {
Err ValidationError
Err *ValidationError
Expected string
}{
{
@@ -117,7 +117,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].(*ValidationError).Field; e != a {
t.Errorf("expected %s, got %s", e, a)
}
}