Rename api ErrorList for clarity
This commit is contained in:
@@ -92,7 +92,7 @@ func NewConflict(kind, name string, err error) error {
|
||||
}
|
||||
|
||||
// NewInvalid returns an error indicating the item is invalid and cannot be processed.
|
||||
func NewInvalid(kind, name string, errs ErrorList) 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 {
|
||||
|
@@ -116,7 +116,7 @@ func TestNewInvalid(t *testing.T) {
|
||||
for i, testCase := range testCases {
|
||||
vErr, expected := testCase.Err, testCase.Details
|
||||
expected.Causes[0].Message = vErr.Error()
|
||||
err := NewInvalid("kind", "name", ErrorList{vErr})
|
||||
err := NewInvalid("kind", "name", ValidationErrorList{vErr})
|
||||
status := err.(*statusError).Status()
|
||||
if status.Code != 422 || status.Reason != api.StatusReasonInvalid {
|
||||
t.Errorf("%d: unexpected status: %#v", i, status)
|
||||
|
@@ -69,5 +69,5 @@ func InterpretDeleteError(err error, kind, name string) error {
|
||||
// for a failure to convert the resource version of an object sent
|
||||
// to the API to an etcd uint64 index.
|
||||
func InterpretResourceVersionError(err error, kind, value string) error {
|
||||
return errors.NewInvalid(kind, "", errors.ErrorList{errors.NewFieldInvalid("resourceVersion", value)})
|
||||
return errors.NewInvalid(kind, "", errors.ValidationErrorList{errors.NewFieldInvalid("resourceVersion", value)})
|
||||
}
|
||||
|
@@ -113,20 +113,22 @@ func NewFieldNotFound(field string, value interface{}) ValidationError {
|
||||
return ValidationError{ValidationErrorTypeNotFound, field, value}
|
||||
}
|
||||
|
||||
// ErrorList is a collection of errors. This does not implement the error
|
||||
// interface to avoid confusion where an empty ErrorList would still be an
|
||||
// error (non-nil). To produce a single error instance from an ErrorList, use
|
||||
// the ToError() method, which will return nil for an empty ErrorList.
|
||||
type ErrorList util.ErrorList
|
||||
// ValidationErrorList is a collection of ValidationErrors. This does not
|
||||
// implement the error interface to avoid confusion where an empty
|
||||
// ValidationErrorList would still be an error (non-nil). To produce a single
|
||||
// error instance from a ValidationErrorList, use the ToError() method, which
|
||||
// will return nil for an empty ValidationErrorList.
|
||||
type ValidationErrorList util.ErrorList
|
||||
|
||||
// ToError converts an ErrorList into a "normal" error, or nil if the list is empty.
|
||||
func (list ErrorList) ToError() error {
|
||||
// ToError converts a ValidationErrorList into a "normal" error, or nil if the
|
||||
// list is empty.
|
||||
func (list ValidationErrorList) ToError() error {
|
||||
return util.ErrorList(list).ToError()
|
||||
}
|
||||
|
||||
// Prefix adds a prefix to the Field of every ValidationError in the list. Returns
|
||||
// the list for convenience.
|
||||
func (list ErrorList) Prefix(prefix string) ErrorList {
|
||||
// Prefix adds a prefix to the Field of every ValidationError 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 strings.HasPrefix(err.Field, "[") {
|
||||
@@ -137,13 +139,15 @@ func (list ErrorList) Prefix(prefix string) ErrorList {
|
||||
err.Field = prefix
|
||||
}
|
||||
list[i] = err
|
||||
} else {
|
||||
glog.Warningf("ValidationErrorList holds non-ValidationError: %T", list)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// PrefixIndex adds an index to the Field of every ValidationError in the list. Returns
|
||||
// the list for convenience.
|
||||
func (list ErrorList) PrefixIndex(index int) ErrorList {
|
||||
// PrefixIndex adds an index to the Field of every ValidationError in the list.
|
||||
// Returns the list for convenience.
|
||||
func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
|
||||
return list.Prefix(fmt.Sprintf("[%d]", index))
|
||||
}
|
||||
|
@@ -82,7 +82,7 @@ func TestErrListPrefix(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
errList := ErrorList{testCase.Err}
|
||||
errList := ValidationErrorList{testCase.Err}
|
||||
prefix := errList.Prefix("foo")
|
||||
if prefix == nil || len(prefix) != len(errList) {
|
||||
t.Errorf("Prefix should return self")
|
||||
@@ -112,7 +112,7 @@ func TestErrListPrefixIndex(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
errList := ErrorList{testCase.Err}
|
||||
errList := ValidationErrorList{testCase.Err}
|
||||
prefix := errList.PrefixIndex(1)
|
||||
if prefix == nil || len(prefix) != len(errList) {
|
||||
t.Errorf("PrefixIndex should return self")
|
||||
|
Reference in New Issue
Block a user