Differentiate between server error messages and client error messages in kubectl

This commit is contained in:
Brendan Burns
2015-03-03 16:24:29 -08:00
parent 53ec66caf4
commit f505a33998
22 changed files with 142 additions and 114 deletions

View File

@@ -26,7 +26,9 @@ import (
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/evanphx/json-patch"
@@ -34,9 +36,18 @@ import (
"github.com/spf13/cobra"
)
func checkErr(err error) {
func CheckErr(err error) {
if err != nil {
glog.FatalDepth(1, err.Error())
if errors.IsStatusError(err) {
glog.FatalDepth(1, fmt.Sprintf("Error received from API: %s", err.Error()))
}
if errors.IsUnexpectedObjectError(err) {
glog.FatalDepth(1, fmt.Sprintf("Unexpected object received from server: %s", err.Error()))
}
if client.IsUnexpectedStatusError(err) {
glog.FatalDepth(1, fmt.Sprintf("Unexpected status received from server: %s", err.Error()))
}
glog.FatalDepth(1, fmt.Sprintf("Client error processing command: %s", err.Error()))
}
}
@@ -75,7 +86,7 @@ func GetFlagInt(cmd *cobra.Command, flag string) int {
v, err := strconv.Atoi(f.Value.String())
// This is likely not a sufficiently friendly error message, but cobra
// should prevent non-integer values from reaching here.
checkErr(err)
CheckErr(err)
return v
}
@@ -85,7 +96,7 @@ func GetFlagDuration(cmd *cobra.Command, flag string) time.Duration {
glog.Fatalf("Flag accessed but not defined for command %s: %s", cmd.Name(), flag)
}
v, err := time.ParseDuration(f.Value.String())
checkErr(err)
CheckErr(err)
return v
}

View File

@@ -43,10 +43,10 @@ func ResourceFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTMapper,
}
version, kind, err := mapper.VersionAndKindForResource(resource)
checkErr(err)
CheckErr(err)
mapping, err = mapper.RESTMapping(kind, version)
checkErr(err)
CheckErr(err)
return
}
@@ -56,37 +56,37 @@ func ResourceFromArgs(cmd *cobra.Command, args []string, mapper meta.RESTMapper,
// DEPRECATED: Use resource.Builder
func ResourceFromFile(filename string, typer runtime.ObjectTyper, mapper meta.RESTMapper, schema validation.Schema, cmdVersion string) (mapping *meta.RESTMapping, namespace, name string, data []byte) {
configData, err := ReadConfigData(filename)
checkErr(err)
CheckErr(err)
data = configData
objVersion, kind, err := typer.DataVersionAndKind(data)
checkErr(err)
CheckErr(err)
// TODO: allow unversioned objects?
if len(objVersion) == 0 {
checkErr(fmt.Errorf("the resource in the provided file has no apiVersion defined"))
CheckErr(fmt.Errorf("the resource in the provided file has no apiVersion defined"))
}
err = schema.ValidateBytes(data)
checkErr(err)
CheckErr(err)
// decode using the version stored with the object (allows codec to vary across versions)
mapping, err = mapper.RESTMapping(kind, objVersion)
checkErr(err)
CheckErr(err)
obj, err := mapping.Codec.Decode(data)
checkErr(err)
CheckErr(err)
meta := mapping.MetadataAccessor
namespace, err = meta.Namespace(obj)
checkErr(err)
CheckErr(err)
name, err = meta.Name(obj)
checkErr(err)
CheckErr(err)
// if the preferred API version differs, get a different mapper
if cmdVersion != objVersion {
mapping, err = mapper.RESTMapping(kind, cmdVersion)
checkErr(err)
CheckErr(err)
}
return