Merge pull request #33024 from juanvallejo/jvallejo/add-print-error-w-causes-cmdutil-helper

Automatic merge from submit-queue

Add "PrintErrorWithCauses" cmdutil helper

**Release note**:
```release-note
NONE
```

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.
pull/6/head
Kubernetes Submit Queue 2016-10-30 01:12:13 -07:00 committed by GitHub
commit 7778ee2190
2 changed files with 25 additions and 0 deletions

View File

@ -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
}
}
}

View File

@ -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 {