Merge pull request #2781 from commonlisp/master

simplify util.ErrorList by eliminating errorListInternal
This commit is contained in:
Joe Beda 2014-12-16 15:43:55 -08:00
commit 93ed9173e3
9 changed files with 30 additions and 44 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
// StatusError is an error intended for consumption by a REST API server; it can also be // StatusError is an error intended for consumption by a REST API server; it can also be
@ -126,7 +127,7 @@ func NewInvalid(kind, name string, errs ValidationErrorList) error {
ID: name, ID: name,
Causes: causes, Causes: causes,
}, },
Message: fmt.Sprintf("%s %q is invalid: %v", kind, name, errs.ToError()), Message: fmt.Sprintf("%s %q is invalid: %v", kind, name, util.SliceToError(errs)),
}} }}
} }

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog" "github.com/golang/glog"
) )
@ -121,18 +120,7 @@ func NewFieldNotFound(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeNotFound, field, value, ""} return &ValidationError{ValidationErrorTypeNotFound, field, value, ""}
} }
// ValidationErrorList is a collection of ValidationErrors. This does not type ValidationErrorList []error
// 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 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. // Prefix adds a prefix to the Field of every ValidationError in the list.
// Returns the list for convenience. // Returns the list for convenience.

View File

@ -851,7 +851,7 @@ func TestValidateService(t *testing.T) {
registry.List = tc.existing registry.List = tc.existing
errs := ValidateService(&tc.svc, registry, api.NewDefaultContext()) errs := ValidateService(&tc.svc, registry, api.NewDefaultContext())
if len(errs) != tc.numErrs { if len(errs) != tc.numErrs {
t.Errorf("Unexpected error list for case %q: %v", tc.name, errs.ToError()) t.Errorf("Unexpected error list for case %q: %v", tc.name, util.SliceToError(errs))
} }
} }

View File

@ -23,7 +23,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
) )
type RESTClientPoster interface { type RESTClientPoster interface {
@ -37,8 +36,8 @@ type ClientPosterFunc func(mapping *meta.RESTMapping) (RESTClientPoster, error)
// be valid API type. It requires ObjectTyper to parse the Version and Kind and // be valid API type. It requires ObjectTyper to parse the Version and Kind and
// RESTMapper to get the resource URI and REST client that knows how to create // RESTMapper to get the resource URI and REST client that knows how to create
// given type // given type
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientPosterFunc, objects []runtime.Object) util.ErrorList { func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientPosterFunc, objects []runtime.Object) []error {
allErrors := util.ErrorList{} var allErrors []error
for i, obj := range objects { for i, obj := range objects {
version, kind, err := typer.ObjectVersionAndKind(obj) version, kind, err := typer.ObjectVersionAndKind(obj)
if err != nil { if err != nil {

View File

@ -23,14 +23,13 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/config"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// DataToObjects converts the raw JSON data into API objects // DataToObjects converts the raw JSON data into API objects
func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (result []runtime.Object, errors util.ErrorList) { func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (result []runtime.Object, errors []error) {
configObj := []runtime.RawExtension{} configObj := []runtime.RawExtension{}
if err := yaml.Unmarshal(data, &configObj); err != nil { if err := yaml.Unmarshal(data, &configObj); err != nil {

View File

@ -20,18 +20,17 @@ import (
"fmt" "fmt"
) )
// ErrorList is a collection of errors. This does not implement the error // []error is a collection of errors. This does not implement the error
// interface to avoid confusion where an empty ErrorList would still be an // interface to avoid confusion where an empty []error would still be an
// error (non-nil). To produce a single error instance from an ErrorList, use // error (non-nil). To produce a single error instance from an []error, use
// the ToError() method, which will return nil for an empty ErrorList. // the SliceToError() method, which will return nil for an empty []error.
type ErrorList []error
// This helper implements the error interface for ErrorList, but prevents // This helper implements the error interface for []error, but prevents
// accidental conversion of ErrorList to error. // accidental conversion of []error to error.
type errorListInternal ErrorList type errorList []error
// Error is part of the error interface. // Error is part of the error interface.
func (list errorListInternal) Error() string { func (list errorList) Error() string {
if len(list) == 0 { if len(list) == 0 {
return "" return ""
} }
@ -46,10 +45,10 @@ func (list errorListInternal) Error() string {
return result return result
} }
// ToError converts an ErrorList into a "normal" error, or nil if the list is empty. // SliceToError converts an []error into a "normal" error, or nil if the slice is empty.
func (list ErrorList) ToError() error { func SliceToError(errs []error) error {
if len(list) == 0 { if len(errs) == 0 {
return nil return nil
} }
return errorListInternal(list) return errorList(errs)
} }

View File

@ -22,24 +22,24 @@ import (
) )
func TestErrorList(t *testing.T) { func TestErrorList(t *testing.T) {
errList := ErrorList{} var errList []error
err := errList.ToError() err := SliceToError(errList)
if err != nil { if err != nil {
t.Errorf("expected nil, got %v", err) t.Errorf("expected nil, got %v", err)
} }
if a := errorListInternal(errList).Error(); a != "" { if a := errorList(errList).Error(); a != "" {
t.Errorf("expected empty string, got %q", a) t.Errorf("expected empty string, got %q", a)
} }
testCases := []struct { testCases := []struct {
errs ErrorList errs []error
expected string expected string
}{ }{
{ErrorList{fmt.Errorf("abc")}, "abc"}, {[]error{fmt.Errorf("abc")}, "abc"},
{ErrorList{fmt.Errorf("abc"), fmt.Errorf("123")}, "[abc, 123]"}, {[]error{fmt.Errorf("abc"), fmt.Errorf("123")}, "[abc, 123]"},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
err := testCase.errs.ToError() err := SliceToError(testCase.errs)
if err == nil { if err == nil {
t.Errorf("expected an error, got nil: %v", testCase) t.Errorf("expected an error, got nil: %v", testCase)
continue continue

View File

@ -35,7 +35,7 @@ func New(authRequestHandlers ...authenticator.Request) authenticator.Request {
// AuthenticateRequest authenticates the request using a chain of authenticator.Request objects. The first // AuthenticateRequest authenticates the request using a chain of authenticator.Request objects. The first
// success returns that identity. Errors are only returned if no matches are found. // success returns that identity. Errors are only returned if no matches are found.
func (authHandler unionAuthRequestHandler) AuthenticateRequest(req *http.Request) (user.Info, bool, error) { func (authHandler unionAuthRequestHandler) AuthenticateRequest(req *http.Request) (user.Info, bool, error) {
var errors util.ErrorList var errors []error
for _, currAuthRequestHandler := range authHandler { for _, currAuthRequestHandler := range authHandler {
info, ok, err := currAuthRequestHandler.AuthenticateRequest(req) info, ok, err := currAuthRequestHandler.AuthenticateRequest(req)
if err != nil { if err != nil {
@ -48,5 +48,5 @@ func (authHandler unionAuthRequestHandler) AuthenticateRequest(req *http.Request
} }
} }
return nil, false, errors.ToError() return nil, false, util.SliceToError(errors)
} }

View File

@ -55,7 +55,7 @@ func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool,
return nil, false, nil return nil, false, nil
} }
var errors util.ErrorList var errors []error
for _, cert := range req.TLS.PeerCertificates { for _, cert := range req.TLS.PeerCertificates {
chains, err := cert.Verify(a.opts) chains, err := cert.Verify(a.opts)
if err != nil { if err != nil {
@ -75,7 +75,7 @@ func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool,
} }
} }
} }
return nil, false, errors.ToError() return nil, false, util.SliceToError(errors)
} }
// DefaultVerifyOptions returns VerifyOptions that use the system root certificates, current time, // DefaultVerifyOptions returns VerifyOptions that use the system root certificates, current time,