Unwrap aggregates of size 1 when writing errors

Also, only use aggregate in get when we actually are dealing with
multiple errors.
This commit is contained in:
Clayton Coleman 2016-09-10 18:37:27 -04:00
parent 19a2a10354
commit 9bfa63dbba
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
3 changed files with 37 additions and 17 deletions

View File

@ -301,22 +301,23 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm
return err return err
} }
allErrs := []error{}
singular := false
infos, err := r.IntoSingular(&singular).Infos()
if err != nil {
if singular {
return err
}
allErrs = append(allErrs, err)
}
// the outermost object will be converted to the output-version, but inner // the outermost object will be converted to the output-version, but inner
// objects can use their mappings // objects can use their mappings
version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion) version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
if err != nil { if err != nil {
return err return err
} }
var errs []error
singular := false
infos, err := r.IntoSingular(&singular).Infos()
if err != nil {
if singular {
return err
}
errs = append(errs, err)
}
res := "" res := ""
if len(infos) > 0 { if len(infos) > 0 {
res = infos[0].ResourceMapping().Resource res = infos[0].ResourceMapping().Resource
@ -329,20 +330,20 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm
isList := meta.IsListType(obj) isList := meta.IsListType(obj)
if isList { if isList {
filteredResourceCount, items, errs := cmdutil.FilterResourceList(obj, filterFuncs, filterOpts) filteredResourceCount, items, err := cmdutil.FilterResourceList(obj, filterFuncs, filterOpts)
if errs != nil { if err != nil {
return errs return err
} }
filteredObj, err := cmdutil.ObjectListToVersionedObject(items, version) filteredObj, err := cmdutil.ObjectListToVersionedObject(items, version)
if err != nil { if err != nil {
return err return err
} }
if err := printer.PrintObj(filteredObj, out); err != nil { if err := printer.PrintObj(filteredObj, out); err != nil {
allErrs = append(allErrs, err) errs = append(errs, err)
} }
cmdutil.PrintFilterCount(filteredResourceCount, res, errOut, filterOpts) cmdutil.PrintFilterCount(filteredResourceCount, res, errOut, filterOpts)
return utilerrors.NewAggregate(allErrs) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs)))
} }
filteredResourceCount := 0 filteredResourceCount := 0
@ -350,14 +351,14 @@ func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Comm
if err != nil { if err != nil {
glog.V(2).Infof("Unable to filter resource: %v", err) glog.V(2).Infof("Unable to filter resource: %v", err)
} else if err := printer.PrintObj(obj, out); err != nil { } else if err := printer.PrintObj(obj, out); err != nil {
allErrs = append(allErrs, err) errs = append(errs, err)
} }
} else if isFiltered { } else if isFiltered {
filteredResourceCount++ filteredResourceCount++
} }
cmdutil.PrintFilterCount(filteredResourceCount, res, errOut, filterOpts) cmdutil.PrintFilterCount(filteredResourceCount, res, errOut, filterOpts)
return utilerrors.NewAggregate(allErrs) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs)))
} }
allErrs := []error{} allErrs := []error{}

View File

@ -122,6 +122,11 @@ func checkErrWithPrefix(prefix string, err error) {
// checkErr formats a given error as a string and calls the passed handleErr // checkErr formats a given error as a string and calls the passed handleErr
// func with that string and an kubectl exit code. // func with that string and an kubectl exit code.
func checkErr(prefix string, err error, handleErr func(string, int)) { func checkErr(prefix string, err error, handleErr func(string, int)) {
// unwrap aggregates of 1
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) == 1 {
err = agg.Errors()[0]
}
switch { switch {
case err == nil: case err == nil:
return return

View File

@ -147,6 +147,20 @@ func Flatten(agg Aggregate) Aggregate {
return NewAggregate(result) return NewAggregate(result)
} }
// Reduce will return err or, if err is an Aggregate and only has one item,
// the first item in the aggregate.
func Reduce(err error) error {
if agg, ok := err.(Aggregate); ok && err != nil {
switch len(agg.Errors()) {
case 1:
return agg.Errors()[0]
case 0:
return nil
}
}
return err
}
// AggregateGoroutines runs the provided functions in parallel, stuffing all // AggregateGoroutines runs the provided functions in parallel, stuffing all
// non-nil errors into the returned Aggregate. // non-nil errors into the returned Aggregate.
// Returns nil if all the functions complete successfully. // Returns nil if all the functions complete successfully.