diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index 92195a75013..a38e04de758 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -53,6 +53,10 @@ type debugError interface { // This method is generic to the command in use and may be used by non-Kubectl // commands. func CheckErr(err error) { + checkErr(err, fatal) +} + +func checkErr(err error, handleErr func(string)) { if err == nil { return } @@ -61,22 +65,22 @@ func CheckErr(err error) { details := err.(*errors.StatusError).Status().Details prefix := fmt.Sprintf("The %s %q is invalid:", details.Kind, details.Name) errs := statusCausesToAggrError(details.Causes) - fatal(MultilineError(prefix, errs)) + handleErr(MultilineError(prefix, errs)) } // handle multiline errors if clientcmd.IsConfigurationInvalid(err) { - fatal(MultilineError("Error in configuration: ", err)) + handleErr(MultilineError("Error in configuration: ", err)) } if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 { - fatal(MultipleErrors("", agg.Errors())) + handleErr(MultipleErrors("", agg.Errors())) } msg, ok := StandardErrorMessage(err) if !ok { msg = fmt.Sprintf("error: %s\n", err.Error()) } - fatal(msg) + handleErr(msg) } func statusCausesToAggrError(scs []api.StatusCause) utilerrors.Aggregate { diff --git a/pkg/kubectl/cmd/util/helpers_test.go b/pkg/kubectl/cmd/util/helpers_test.go index cae9b98fd49..cf1e36988da 100644 --- a/pkg/kubectl/cmd/util/helpers_test.go +++ b/pkg/kubectl/cmd/util/helpers_test.go @@ -25,7 +25,9 @@ import ( "testing" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" + "github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors" ) func TestMerge(t *testing.T) { @@ -399,3 +401,36 @@ func TestReadConfigData(t *testing.T) { } } } + +func TestCheckInvalidErr(t *testing.T) { + tests := []struct { + err error + expected string + }{ + { + errors.NewInvalid("Invalid1", "invalidation", fielderrors.ValidationErrorList{fielderrors.NewFieldInvalid("Cause", "single", "details")}), + `Error from server: Invalid1 "invalidation" is invalid: Cause: invalid value 'single': details`, + }, + { + errors.NewInvalid("Invalid2", "invalidation", fielderrors.ValidationErrorList{fielderrors.NewFieldInvalid("Cause", "multi1", "details"), fielderrors.NewFieldInvalid("Cause", "multi2", "details")}), + `Error from server: Invalid2 "invalidation" is invalid: [Cause: invalid value 'multi1': details, Cause: invalid value 'multi2': details]`, + }, + { + errors.NewInvalid("Invalid3", "invalidation", fielderrors.ValidationErrorList{}), + `Error from server: Invalid3 "invalidation" is invalid: `, + }, + } + + var errReturned string + errHandle := func(err string) { + errReturned = err + } + + for _, test := range tests { + checkErr(test.err, errHandle) + + if errReturned != test.expected { + t.Fatalf("Got: %s, expected: %s", errReturned, test.expected) + } + } +}