improve the error message of update pod
This commit is contained in:
@@ -45,7 +45,7 @@ func NewCmdPatch(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
err := RunPatch(f, out, cmd, args, shortOutput)
|
||||
cmdutil.CheckCustomErr("Patch failed", err)
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringP("patch", "p", "", "The patch to be applied to the resource JSON file.")
|
||||
|
@@ -59,7 +59,7 @@ func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
err := RunReplace(f, out, cmd, args, filenames, shortOutput)
|
||||
cmdutil.CheckCustomErr("Replace failed", err)
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
}
|
||||
usage := "Filename, directory, or URL to file to use to replace the resource."
|
||||
|
@@ -79,59 +79,7 @@ func checkErr(err error, handleErr func(string)) {
|
||||
|
||||
if errors.IsInvalid(err) {
|
||||
details := err.(*errors.StatusError).Status().Details
|
||||
prefix := fmt.Sprintf("The %s %q is invalid:", details.Kind, details.Name)
|
||||
errs := statusCausesToAggrError(details.Causes)
|
||||
handleErr(MultilineError(prefix, errs))
|
||||
}
|
||||
|
||||
// handle multiline errors
|
||||
if clientcmd.IsConfigurationInvalid(err) {
|
||||
handleErr(MultilineError("Error in configuration: ", err))
|
||||
}
|
||||
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) > 0 {
|
||||
handleErr(MultipleErrors("", agg.Errors()))
|
||||
}
|
||||
|
||||
msg, ok := StandardErrorMessage(err)
|
||||
if !ok {
|
||||
msg = fmt.Sprintf("error: %s\n", err.Error())
|
||||
}
|
||||
handleErr(msg)
|
||||
}
|
||||
|
||||
// CheckCustomErr is like CheckErr except a custom prefix error
|
||||
// string may be provied to help produce more specific error messages.
|
||||
// For example, for the update failed case this function could be called
|
||||
// with:
|
||||
// cmdutil.CheckCustomErr("Update failed", err)
|
||||
// This function supresses the detailed output that is produced by CheckErr
|
||||
// and specifically the field is erased and the error message has the details
|
||||
// of the spec removed. Unfortunately, what starts off as a detail message is
|
||||
// a sperate field ends up being concatentated into one string which contains
|
||||
// the spec and the detail string. To avoid significant refactoring of the error
|
||||
// data structures we just extract the required detail string by looking for it
|
||||
// after "}': " which is horrible but expedient.
|
||||
func CheckCustomErr(customPrefix string, err error) {
|
||||
checkCustomErr(customPrefix, err, fatal)
|
||||
}
|
||||
|
||||
func checkCustomErr(customPrefix string, err error, handleErr func(string)) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if errors.IsInvalid(err) {
|
||||
details := err.(*errors.StatusError).Status().Details
|
||||
for i := range details.Causes {
|
||||
c := &details.Causes[i]
|
||||
s := strings.Split(c.Message, "}': ")
|
||||
if len(s) == 2 {
|
||||
c.Message =
|
||||
s[1]
|
||||
c.Field = ""
|
||||
}
|
||||
}
|
||||
prefix := fmt.Sprintf("%s", customPrefix)
|
||||
prefix := fmt.Sprintf("The %s %q is invalid.\n", details.Kind, details.Name)
|
||||
errs := statusCausesToAggrError(details.Causes)
|
||||
handleErr(MultilineError(prefix, errs))
|
||||
}
|
||||
|
@@ -274,11 +274,11 @@ func TestCheckInvalidErr(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
errors.NewInvalid("Invalid1", "invalidation", fielderrors.ValidationErrorList{fielderrors.NewFieldInvalid("Cause", "single", "details")}),
|
||||
`Error from server: Invalid1 "invalidation" is invalid: Cause: invalid value 'single': details`,
|
||||
`Error from server: Invalid1 "invalidation" is invalid: Cause: invalid value 'single', Details: 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]`,
|
||||
`Error from server: Invalid2 "invalidation" is invalid: [Cause: invalid value 'multi1', Details: details, Cause: invalid value 'multi2', Details: details]`,
|
||||
},
|
||||
{
|
||||
errors.NewInvalid("Invalid3", "invalidation", fielderrors.ValidationErrorList{}),
|
||||
|
Reference in New Issue
Block a user