Skip to content

Commit

Permalink
Merge PluginError into Result
Browse files Browse the repository at this point in the history
Signed-off-by: Deep Debroy <ddebroy@apple.com>
  • Loading branch information
Deep Debroy committed Jan 6, 2021
1 parent eb1350a commit 101b775
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
25 changes: 16 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,27 @@ func (c *Client) invokePlugin(ctx context.Context, name string, r *types.Request
cmd.Stderr = os.Stderr

out, err := cmd.Output()
msg := "output:"
if len(out) > 0 {
msg = fmt.Sprintf("%s %q", msg, out)
} else {
msg = fmt.Sprintf("%s <empty>", msg)
}
if err != nil {
// ExitError is returned when there is a non-zero exit status
if _, ok := err.(*exec.ExitError); ok {
var pe types.PluginError
if err := json.Unmarshal(out, &pe); err != nil {
return nil, errors.Wrapf(err, "%s: %s", name, out)
}
return nil, &pe
if exitErr, ok := err.(*exec.ExitError); ok {
// ExitError is returned when there is a non-zero exit status
msg = fmt.Sprintf("%s exit code: %d", msg, exitErr.ExitCode())
} else {
// plugin did not get a chance to run, return exec err
return nil, err
}
return nil, errors.Wrapf(err, "%s: %s", name, out)
}
var result types.Result
if err := json.Unmarshal(out, &result); err != nil {
return nil, err
return nil, errors.Errorf("failed to unmarshal plugin output: %s: %s", err.Error(), msg)
}
if result.Err() != nil {
return nil, result.Err()
}
return &result, nil
}
Expand Down
20 changes: 12 additions & 8 deletions skel/skel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ func Run(ctx context.Context, plugin Plugin) error {
case "invoke":
result, err := plugin.Invoke(ctx, &request)
if err != nil {
pe := types.NewPluginError(plugin.Type(), err)
if err := enc.Encode(pe); err != nil {
return err
}
return pe
// if the plugin sets ErrorMessage we ignore it
result = request.NewResult(plugin.Type())
result.Error = err.Error()
}
if err := enc.Encode(result); err != nil {
return errors.Wrap(err, "unable to encode plugin error to stdout")
}
out = result
default:
return errors.New("undefined arg")
result := request.NewResult(plugin.Type())
result.Error = fmt.Sprintf("invalid arg %s", os.Args[1])
if err := enc.Encode(result); err != nil {
return errors.Wrap(err, "unable to encode invalid parameter error to stdout")
}
}
return enc.Encode(out)
return nil
}
33 changes: 11 additions & 22 deletions types/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v1

import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)

// Plugin type and configuration
Expand Down Expand Up @@ -81,27 +81,6 @@ type Request struct {
Results []*Result `json:"results,omitempty"`
}

// NewPluginError returns a plugin error
func NewPluginError(name string, err error) error {
return &PluginError{
Plugin: name,
Message: err.Error(),
}
}

// PluginError for specific plugin execution
type PluginError struct {
// Plugin name
Plugin string `json:"plugin"`
// Message for the error
Message string `json:"message"`
}

// Error as a string
func (p *PluginError) Error() string {
return fmt.Sprintf("%s: %s", p.Plugin, p.Message)
}

// IsSandbox returns true if the request is for a sandbox
func (r *Request) IsSandbox() bool {
return r.ID == r.SandboxID
Expand All @@ -122,6 +101,16 @@ type Result struct {
Plugin string `json:"plugin"`
// Version of the plugin
Version string `json:"version"`
// Error message in case of failures
Error string `json:"error"`
// Metadata specific to actions taken by the plugin
Metadata map[string]string `json:"metadata,omitempty"`
}

// Err creates an Error object if ErrorMessage is populated
func (r *Result) Err() error {
if r.Error != "" {
return errors.New(r.Error)
}
return nil
}

0 comments on commit 101b775

Please sign in to comment.