From 643649113fe54b641005d24e143bb9b07df70ff8 Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Mon, 19 Sep 2016 11:51:47 -0400 Subject: [PATCH] Add "PrintErrorWithCauses" cmdutil helper This patch adds a new helper function to cmd/util/helpers.go that handles errors containing collections of causes and prints each cause in a separate newline. --- pkg/kubectl/cmd/set/helper.go | 5 +++++ pkg/kubectl/cmd/util/helpers.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/kubectl/cmd/set/helper.go b/pkg/kubectl/cmd/set/helper.go index 8c01d66079..ddf1a6ee11 100644 --- a/pkg/kubectl/cmd/set/helper.go +++ b/pkg/kubectl/cmd/set/helper.go @@ -23,6 +23,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" + kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/util/strategicpatch" @@ -59,6 +60,10 @@ func handlePodUpdateError(out io.Writer, err error, resource string) { if all && match { return } + } else { + if ok := kcmdutil.PrintErrorWithCauses(err, out); ok { + return + } } } diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index 3dba6ba126..5c8e972bd6 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -255,6 +255,26 @@ func MultilineError(prefix string, err error) string { return fmt.Sprintf("%s%s\n", prefix, err) } +// PrintErrorWithCauses prints an error's kind, name, and each of the error's causes in a new line. +// The returned string will end with a newline. +// Returns true if a case exists to handle the error type, or false otherwise. +func PrintErrorWithCauses(err error, errOut io.Writer) bool { + switch t := err.(type) { + case *kerrors.StatusError: + errorDetails := t.Status().Details + if errorDetails != nil { + fmt.Fprintf(errOut, "error: %s %q is invalid\n\n", errorDetails.Kind, errorDetails.Name) + for _, cause := range errorDetails.Causes { + fmt.Fprintf(errOut, "* %s: %s\n", cause.Field, cause.Message) + } + return true + } + } + + fmt.Fprintf(errOut, "error: %v\n", err) + return false +} + // MultipleErrors returns a newline delimited string containing // the prefix and referenced errors in standard form. func MultipleErrors(prefix string, errs []error) string {