Add source in “error from server” message when using kubectl

This commit is contained in:
feihujiang 2015-06-23 20:26:27 +08:00
parent ebeb104493
commit b41b53122a
6 changed files with 26 additions and 7 deletions

View File

@ -97,11 +97,11 @@ func RunCreate(f *cmdutil.Factory, out io.Writer, filenames util.StringList) err
err = r.Visit(func(info *resource.Info) error { err = r.Visit(func(info *resource.Info) error {
data, err := info.Mapping.Codec.Encode(info.Object) data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return err return cmdutil.AddSourceToErr("creating", info.Source, err)
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data) obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, data)
if err != nil { if err != nil {
return err return cmdutil.AddSourceToErr("creating", info.Source, err)
} }
count++ count++
info.Refresh(obj, true) info.Refresh(obj, true)

View File

@ -122,14 +122,14 @@ func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefault
if kubectl.IsNoSuchReaperError(err) && isDefaultDelete { if kubectl.IsNoSuchReaperError(err) && isDefaultDelete {
return deleteResource(info, out) return deleteResource(info, out)
} }
return err return cmdutil.AddSourceToErr("reaping", info.Source, err)
} }
var options *api.DeleteOptions var options *api.DeleteOptions
if gracePeriod >= 0 { if gracePeriod >= 0 {
options = api.NewDeleteOptions(int64(gracePeriod)) options = api.NewDeleteOptions(int64(gracePeriod))
} }
if _, err := reaper.Stop(info.Namespace, info.Name, timeout, options); err != nil { if _, err := reaper.Stop(info.Namespace, info.Name, timeout, options); err != nil {
return err return cmdutil.AddSourceToErr("stopping", info.Source, err)
} }
fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name) fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name)
return nil return nil
@ -163,7 +163,7 @@ func DeleteResult(r *resource.Result, out io.Writer, ignoreNotFound bool) error
func deleteResource(info *resource.Info, out io.Writer) error { func deleteResource(info *resource.Info, out io.Writer) error {
if err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name); err != nil { if err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name); err != nil {
return err return cmdutil.AddSourceToErr("deleting", info.Source, err)
} }
fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name) fmt.Fprintf(out, "%s/%s\n", info.Mapping.Resource, info.Name)
return nil return nil

View File

@ -111,11 +111,11 @@ func RunUpdate(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
return r.Visit(func(info *resource.Info) error { return r.Visit(func(info *resource.Info) error {
data, err := info.Mapping.Codec.Encode(info.Object) data, err := info.Mapping.Codec.Encode(info.Object)
if err != nil { if err != nil {
return err return cmdutil.AddSourceToErr("updating", info.Source, err)
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Update(info.Namespace, info.Name, true, data) obj, err := resource.NewHelper(info.Client, info.Mapping).Update(info.Namespace, info.Name, true, data)
if err != nil { if err != nil {
return err return cmdutil.AddSourceToErr("updating", info.Source, err)
} }
info.Refresh(obj, true) info.Refresh(obj, true)
printObjectSpecificMessage(obj, out) printObjectSpecificMessage(obj, out)

View File

@ -48,6 +48,21 @@ type debugError interface {
DebugError() (msg string, args []interface{}) DebugError() (msg string, args []interface{})
} }
// AddSourceToErr adds handleResourcePrefix and source string to error message.
// verb is the string like "creating", "deleting" etc.
// souce is the filename or URL to the template file(*.json or *.yaml), or stdin to use to handle the resource.
func AddSourceToErr(verb string, source string, err error) error {
if source != "" {
if statusError, ok := err.(*errors.StatusError); ok {
status := statusError.Status()
status.Message = fmt.Sprintf("error when %s %q: %v", verb, source, status.Message)
return &errors.StatusError{status}
}
return fmt.Errorf("error when %s %q: %v", verb, source, err)
}
return err
}
// CheckErr prints a user friendly error to STDERR and exits with a non-zero // CheckErr prints a user friendly error to STDERR and exits with a non-zero
// exit code. Unrecognized errors will be printed with an "error: " prefix. // exit code. Unrecognized errors will be printed with an "error: " prefix.
// //

View File

@ -69,6 +69,7 @@ func (m *Mapper) InfoForData(data []byte, source string) (*Info, error) {
Client: client, Client: client,
Namespace: namespace, Namespace: namespace,
Name: name, Name: name,
Source: source,
Object: obj, Object: obj,
ResourceVersion: resourceVersion, ResourceVersion: resourceVersion,

View File

@ -66,6 +66,9 @@ type Info struct {
Namespace string Namespace string
Name string Name string
// Optional, Source is the filename or URL to template file (.json or .yaml),
// or stdin to use to handle the resource
Source string
// Optional, this is the most recent value returned by the server if available // Optional, this is the most recent value returned by the server if available
runtime.Object runtime.Object
// Optional, this is the most recent resource version the server knows about for // Optional, this is the most recent resource version the server knows about for