Rename api ErrorList for clarity

This commit is contained in:
Tim Hockin 2014-10-24 09:43:14 -07:00
parent 2bbd11eda6
commit d6effe3c6d
7 changed files with 74 additions and 70 deletions

View File

@ -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. // 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)) causes := make([]api.StatusCause, 0, len(errs))
for i := range errs { for i := range errs {
if err, ok := errs[i].(ValidationError); ok { if err, ok := errs[i].(ValidationError); ok {

View File

@ -116,7 +116,7 @@ func TestNewInvalid(t *testing.T) {
for i, testCase := range testCases { for i, testCase := range testCases {
vErr, expected := testCase.Err, testCase.Details vErr, expected := testCase.Err, testCase.Details
expected.Causes[0].Message = vErr.Error() expected.Causes[0].Message = vErr.Error()
err := NewInvalid("kind", "name", ErrorList{vErr}) err := NewInvalid("kind", "name", ValidationErrorList{vErr})
status := err.(*statusError).Status() status := err.(*statusError).Status()
if status.Code != 422 || status.Reason != api.StatusReasonInvalid { if status.Code != 422 || status.Reason != api.StatusReasonInvalid {
t.Errorf("%d: unexpected status: %#v", i, status) t.Errorf("%d: unexpected status: %#v", i, status)

View File

@ -69,5 +69,5 @@ func InterpretDeleteError(err error, kind, name string) error {
// for a failure to convert the resource version of an object sent // for a failure to convert the resource version of an object sent
// to the API to an etcd uint64 index. // to the API to an etcd uint64 index.
func InterpretResourceVersionError(err error, kind, value string) error { 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)})
} }

View File

@ -113,20 +113,22 @@ func NewFieldNotFound(field string, value interface{}) ValidationError {
return ValidationError{ValidationErrorTypeNotFound, field, value} return ValidationError{ValidationErrorTypeNotFound, field, value}
} }
// ErrorList is a collection of errors. This does not implement the error // ValidationErrorList is a collection of ValidationErrors. This does not
// interface to avoid confusion where an empty ErrorList would still be an // implement the error interface to avoid confusion where an empty
// error (non-nil). To produce a single error instance from an ErrorList, use // ValidationErrorList would still be an error (non-nil). To produce a single
// the ToError() method, which will return nil for an empty ErrorList. // error instance from a ValidationErrorList, use the ToError() method, which
type ErrorList util.ErrorList // 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. // ToError converts a ValidationErrorList into a "normal" error, or nil if the
func (list ErrorList) ToError() error { // list is empty.
func (list ValidationErrorList) ToError() error {
return util.ErrorList(list).ToError() return util.ErrorList(list).ToError()
} }
// Prefix adds a prefix to the Field of every ValidationError in the list. Returns // Prefix adds a prefix to the Field of every ValidationError in the list.
// the list for convenience. // Returns the list for convenience.
func (list ErrorList) Prefix(prefix string) ErrorList { func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
for i := range list { for i := range list {
if err, ok := list[i].(ValidationError); ok { if err, ok := list[i].(ValidationError); ok {
if strings.HasPrefix(err.Field, "[") { if strings.HasPrefix(err.Field, "[") {
@ -137,13 +139,15 @@ func (list ErrorList) Prefix(prefix string) ErrorList {
err.Field = prefix err.Field = prefix
} }
list[i] = err list[i] = err
} else {
glog.Warningf("ValidationErrorList holds non-ValidationError: %T", list)
} }
} }
return list return list
} }
// PrefixIndex adds an index to the Field of every ValidationError in the list. Returns // PrefixIndex adds an index to the Field of every ValidationError in the list.
// the list for convenience. // Returns the list for convenience.
func (list ErrorList) PrefixIndex(index int) ErrorList { func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
return list.Prefix(fmt.Sprintf("[%d]", index)) return list.Prefix(fmt.Sprintf("[%d]", index))
} }

View File

@ -82,7 +82,7 @@ func TestErrListPrefix(t *testing.T) {
}, },
} }
for _, testCase := range testCases { for _, testCase := range testCases {
errList := ErrorList{testCase.Err} errList := ValidationErrorList{testCase.Err}
prefix := errList.Prefix("foo") prefix := errList.Prefix("foo")
if prefix == nil || len(prefix) != len(errList) { if prefix == nil || len(prefix) != len(errList) {
t.Errorf("Prefix should return self") t.Errorf("Prefix should return self")
@ -112,7 +112,7 @@ func TestErrListPrefixIndex(t *testing.T) {
}, },
} }
for _, testCase := range testCases { for _, testCase := range testCases {
errList := ErrorList{testCase.Err} errList := ValidationErrorList{testCase.Err}
prefix := errList.PrefixIndex(1) prefix := errList.PrefixIndex(1)
if prefix == nil || len(prefix) != len(errList) { if prefix == nil || len(prefix) != len(errList) {
t.Errorf("PrefixIndex should return self") t.Errorf("PrefixIndex should return self")

View File

@ -27,13 +27,13 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func validateVolumes(volumes []api.Volume) (util.StringSet, errs.ErrorList) { func validateVolumes(volumes []api.Volume) (util.StringSet, errs.ValidationErrorList) {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range volumes { for i := range volumes {
vol := &volumes[i] // so we can set default values vol := &volumes[i] // so we can set default values
el := errs.ErrorList{} el := errs.ValidationErrorList{}
if vol.Source == nil { if vol.Source == nil {
// TODO: Enforce that a source is set once we deprecate the implied form. // TODO: Enforce that a source is set once we deprecate the implied form.
vol.Source = &api.VolumeSource{ vol.Source = &api.VolumeSource{
@ -57,9 +57,9 @@ func validateVolumes(volumes []api.Volume) (util.StringSet, errs.ErrorList) {
return allNames, allErrs return allNames, allErrs
} }
func validateSource(source *api.VolumeSource) errs.ErrorList { func validateSource(source *api.VolumeSource) errs.ValidationErrorList {
numVolumes := 0 numVolumes := 0
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if source.HostDir != nil { if source.HostDir != nil {
numVolumes++ numVolumes++
allErrs = append(allErrs, validateHostDir(source.HostDir).Prefix("hostDirectory")...) allErrs = append(allErrs, validateHostDir(source.HostDir).Prefix("hostDirectory")...)
@ -78,8 +78,8 @@ func validateSource(source *api.VolumeSource) errs.ErrorList {
return allErrs return allErrs
} }
func validateHostDir(hostDir *api.HostDir) errs.ErrorList { func validateHostDir(hostDir *api.HostDir) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if hostDir.Path == "" { if hostDir.Path == "" {
allErrs = append(allErrs, errs.NewNotFound("path", hostDir.Path)) allErrs = append(allErrs, errs.NewNotFound("path", hostDir.Path))
} }
@ -88,8 +88,8 @@ func validateHostDir(hostDir *api.HostDir) errs.ErrorList {
var supportedPortProtocols = util.NewStringSet(string(api.ProtocolTCP), string(api.ProtocolUDP)) var supportedPortProtocols = util.NewStringSet(string(api.ProtocolTCP), string(api.ProtocolUDP))
func validateGCEPersistentDisk(PD *api.GCEPersistentDisk) errs.ErrorList { func validateGCEPersistentDisk(PD *api.GCEPersistentDisk) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if PD.PDName == "" { if PD.PDName == "" {
allErrs = append(allErrs, errs.NewFieldInvalid("PD.PDName", PD.PDName)) allErrs = append(allErrs, errs.NewFieldInvalid("PD.PDName", PD.PDName))
} }
@ -102,12 +102,12 @@ func validateGCEPersistentDisk(PD *api.GCEPersistentDisk) errs.ErrorList {
return allErrs return allErrs
} }
func validatePorts(ports []api.Port) errs.ErrorList { func validatePorts(ports []api.Port) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range ports { for i := range ports {
pErrs := errs.ErrorList{} pErrs := errs.ValidationErrorList{}
port := &ports[i] // so we can set default values port := &ports[i] // so we can set default values
if len(port.Name) > 0 { if len(port.Name) > 0 {
if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) { if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) {
@ -136,11 +136,11 @@ func validatePorts(ports []api.Port) errs.ErrorList {
return allErrs return allErrs
} }
func validateEnv(vars []api.EnvVar) errs.ErrorList { func validateEnv(vars []api.EnvVar) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
for i := range vars { for i := range vars {
vErrs := errs.ErrorList{} vErrs := errs.ValidationErrorList{}
ev := &vars[i] // so we can set default values ev := &vars[i] // so we can set default values
if len(ev.Name) == 0 { if len(ev.Name) == 0 {
vErrs = append(vErrs, errs.NewFieldRequired("name", ev.Name)) vErrs = append(vErrs, errs.NewFieldRequired("name", ev.Name))
@ -153,11 +153,11 @@ func validateEnv(vars []api.EnvVar) errs.ErrorList {
return allErrs return allErrs
} }
func validateVolumeMounts(mounts []api.VolumeMount, volumes util.StringSet) errs.ErrorList { func validateVolumeMounts(mounts []api.VolumeMount, volumes util.StringSet) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
for i := range mounts { for i := range mounts {
mErrs := errs.ErrorList{} mErrs := errs.ValidationErrorList{}
mnt := &mounts[i] // so we can set default values mnt := &mounts[i] // so we can set default values
if len(mnt.Name) == 0 { if len(mnt.Name) == 0 {
mErrs = append(mErrs, errs.NewFieldRequired("name", mnt.Name)) mErrs = append(mErrs, errs.NewFieldRequired("name", mnt.Name))
@ -174,11 +174,11 @@ func validateVolumeMounts(mounts []api.VolumeMount, volumes util.StringSet) errs
// AccumulateUniquePorts runs an extraction function on each Port of each Container, // AccumulateUniquePorts runs an extraction function on each Port of each Container,
// accumulating the results and returning an error if any ports conflict. // accumulating the results and returning an error if any ports conflict.
func AccumulateUniquePorts(containers []api.Container, accumulator map[int]bool, extract func(*api.Port) int) errs.ErrorList { func AccumulateUniquePorts(containers []api.Container, accumulator map[int]bool, extract func(*api.Port) int) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
for ci := range containers { for ci := range containers {
cErrs := errs.ErrorList{} cErrs := errs.ValidationErrorList{}
ctr := &containers[ci] ctr := &containers[ci]
for pi := range ctr.Ports { for pi := range ctr.Ports {
port := extract(&ctr.Ports[pi]) port := extract(&ctr.Ports[pi])
@ -198,29 +198,29 @@ func AccumulateUniquePorts(containers []api.Container, accumulator map[int]bool,
// checkHostPortConflicts checks for colliding Port.HostPort values across // checkHostPortConflicts checks for colliding Port.HostPort values across
// a slice of containers. // a slice of containers.
func checkHostPortConflicts(containers []api.Container) errs.ErrorList { func checkHostPortConflicts(containers []api.Container) errs.ValidationErrorList {
allPorts := map[int]bool{} allPorts := map[int]bool{}
return AccumulateUniquePorts(containers, allPorts, func(p *api.Port) int { return p.HostPort }) return AccumulateUniquePorts(containers, allPorts, func(p *api.Port) int { return p.HostPort })
} }
func validateExecAction(exec *api.ExecAction) errs.ErrorList { func validateExecAction(exec *api.ExecAction) errs.ValidationErrorList {
allErrors := errs.ErrorList{} allErrors := errs.ValidationErrorList{}
if len(exec.Command) == 0 { if len(exec.Command) == 0 {
allErrors = append(allErrors, errs.NewFieldRequired("command", exec.Command)) allErrors = append(allErrors, errs.NewFieldRequired("command", exec.Command))
} }
return allErrors return allErrors
} }
func validateHTTPGetAction(http *api.HTTPGetAction) errs.ErrorList { func validateHTTPGetAction(http *api.HTTPGetAction) errs.ValidationErrorList {
allErrors := errs.ErrorList{} allErrors := errs.ValidationErrorList{}
if len(http.Path) == 0 { if len(http.Path) == 0 {
allErrors = append(allErrors, errs.NewFieldRequired("path", http.Path)) allErrors = append(allErrors, errs.NewFieldRequired("path", http.Path))
} }
return allErrors return allErrors
} }
func validateHandler(handler *api.Handler) errs.ErrorList { func validateHandler(handler *api.Handler) errs.ValidationErrorList {
allErrors := errs.ErrorList{} allErrors := errs.ValidationErrorList{}
if handler.Exec != nil { if handler.Exec != nil {
allErrors = append(allErrors, validateExecAction(handler.Exec).Prefix("exec")...) allErrors = append(allErrors, validateExecAction(handler.Exec).Prefix("exec")...)
} else if handler.HTTPGet != nil { } else if handler.HTTPGet != nil {
@ -231,8 +231,8 @@ func validateHandler(handler *api.Handler) errs.ErrorList {
return allErrors return allErrors
} }
func validateLifecycle(lifecycle *api.Lifecycle) errs.ErrorList { func validateLifecycle(lifecycle *api.Lifecycle) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if lifecycle.PostStart != nil { if lifecycle.PostStart != nil {
allErrs = append(allErrs, validateHandler(lifecycle.PostStart).Prefix("postStart")...) allErrs = append(allErrs, validateHandler(lifecycle.PostStart).Prefix("postStart")...)
} }
@ -242,12 +242,12 @@ func validateLifecycle(lifecycle *api.Lifecycle) errs.ErrorList {
return allErrs return allErrs
} }
func validateContainers(containers []api.Container, volumes util.StringSet) errs.ErrorList { func validateContainers(containers []api.Container, volumes util.StringSet) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range containers { for i := range containers {
cErrs := errs.ErrorList{} cErrs := errs.ValidationErrorList{}
ctr := &containers[i] // so we can set default values ctr := &containers[i] // so we can set default values
capabilities := capabilities.Get() capabilities := capabilities.Get()
if len(ctr.Name) == 0 { if len(ctr.Name) == 0 {
@ -288,8 +288,8 @@ var supportedManifestVersions = util.NewStringSet("v1beta1", "v1beta2")
// This includes checking formatting and uniqueness. It also canonicalizes the // This includes checking formatting and uniqueness. It also canonicalizes the
// structure by setting default values and implementing any backwards-compatibility // structure by setting default values and implementing any backwards-compatibility
// tricks. // tricks.
func ValidateManifest(manifest *api.ContainerManifest) errs.ErrorList { func ValidateManifest(manifest *api.ContainerManifest) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if len(manifest.Version) == 0 { if len(manifest.Version) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("version", manifest.Version)) allErrs = append(allErrs, errs.NewFieldRequired("version", manifest.Version))
@ -303,9 +303,9 @@ func ValidateManifest(manifest *api.ContainerManifest) errs.ErrorList {
return allErrs return allErrs
} }
func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ErrorList { func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ValidationErrorList {
numPolicies := 0 numPolicies := 0
allErrors := errs.ErrorList{} allErrors := errs.ValidationErrorList{}
if restartPolicy.Always != nil { if restartPolicy.Always != nil {
numPolicies++ numPolicies++
} }
@ -324,14 +324,14 @@ func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ErrorList {
return allErrors return allErrors
} }
func ValidatePodState(podState *api.PodState) errs.ErrorList { func ValidatePodState(podState *api.PodState) errs.ValidationErrorList {
allErrs := errs.ErrorList(ValidateManifest(&podState.Manifest)).Prefix("manifest") allErrs := errs.ValidationErrorList(ValidateManifest(&podState.Manifest)).Prefix("manifest")
return allErrs return allErrs
} }
// ValidatePod tests if required fields in the pod are set. // ValidatePod tests if required fields in the pod are set.
func ValidatePod(pod *api.Pod) errs.ErrorList { func ValidatePod(pod *api.Pod) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if len(pod.Name) == 0 { if len(pod.Name) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name", pod.Name)) allErrs = append(allErrs, errs.NewFieldRequired("name", pod.Name))
} }
@ -343,8 +343,8 @@ func ValidatePod(pod *api.Pod) errs.ErrorList {
} }
// ValidatePodUpdate tests to see if the update is legal // ValidatePodUpdate tests to see if the update is legal
func ValidatePodUpdate(newPod, oldPod *api.Pod) errs.ErrorList { func ValidatePodUpdate(newPod, oldPod *api.Pod) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if newPod.Name != oldPod.Name { if newPod.Name != oldPod.Name {
allErrs = append(allErrs, errs.NewFieldInvalid("name", newPod.Name)) allErrs = append(allErrs, errs.NewFieldInvalid("name", newPod.Name))
@ -371,8 +371,8 @@ func ValidatePodUpdate(newPod, oldPod *api.Pod) errs.ErrorList {
} }
// ValidateService tests if required fields in the service are set. // ValidateService tests if required fields in the service are set.
func ValidateService(service *api.Service) errs.ErrorList { func ValidateService(service *api.Service) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if len(service.Name) == 0 { if len(service.Name) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name", service.Name)) allErrs = append(allErrs, errs.NewFieldRequired("name", service.Name))
} else if !util.IsDNS952Label(service.Name) { } else if !util.IsDNS952Label(service.Name) {
@ -396,8 +396,8 @@ func ValidateService(service *api.Service) errs.ErrorList {
} }
// ValidateReplicationController tests if required fields in the replication controller are set. // ValidateReplicationController tests if required fields in the replication controller are set.
func ValidateReplicationController(controller *api.ReplicationController) errs.ErrorList { func ValidateReplicationController(controller *api.ReplicationController) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if len(controller.Name) == 0 { if len(controller.Name) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("name", controller.Name)) allErrs = append(allErrs, errs.NewFieldRequired("name", controller.Name))
} }
@ -409,8 +409,8 @@ func ValidateReplicationController(controller *api.ReplicationController) errs.E
} }
// ValidateReplicationControllerState tests if required fields in the replication controller state are set. // ValidateReplicationControllerState tests if required fields in the replication controller state are set.
func ValidateReplicationControllerState(state *api.ReplicationControllerState) errs.ErrorList { func ValidateReplicationControllerState(state *api.ReplicationControllerState) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
if labels.Set(state.ReplicaSelector).AsSelector().Empty() { if labels.Set(state.ReplicaSelector).AsSelector().Empty() {
allErrs = append(allErrs, errs.NewFieldRequired("replicaSelector", state.ReplicaSelector)) allErrs = append(allErrs, errs.NewFieldRequired("replicaSelector", state.ReplicaSelector))
} }
@ -426,8 +426,8 @@ func ValidateReplicationControllerState(state *api.ReplicationControllerState) e
allErrs = append(allErrs, ValidateReadOnlyPersistentDisks(state.PodTemplate.DesiredState.Manifest.Volumes).Prefix("podTemplate.desiredState.manifest")...) allErrs = append(allErrs, ValidateReadOnlyPersistentDisks(state.PodTemplate.DesiredState.Manifest.Volumes).Prefix("podTemplate.desiredState.manifest")...)
return allErrs return allErrs
} }
func ValidateReadOnlyPersistentDisks(volumes []api.Volume) errs.ErrorList { func ValidateReadOnlyPersistentDisks(volumes []api.Volume) errs.ValidationErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ValidationErrorList{}
for _, vol := range volumes { for _, vol := range volumes {
if vol.Source.GCEPersistentDisk != nil { if vol.Source.GCEPersistentDisk != nil {
if vol.Source.GCEPersistentDisk.ReadOnly == false { if vol.Source.GCEPersistentDisk.ReadOnly == false {

View File

@ -26,7 +26,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
func expectPrefix(t *testing.T, prefix string, errs errors.ErrorList) { func expectPrefix(t *testing.T, prefix string, errs errors.ValidationErrorList) {
for i := range errs { for i := range errs {
if f, p := errs[i].(errors.ValidationError).Field, prefix; !strings.HasPrefix(f, p) { if f, p := errs[i].(errors.ValidationError).Field, prefix; !strings.HasPrefix(f, p) {
t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i]) t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i])