Skip to content

Commit

Permalink
cli-plugins/manager: simplify generating errors
Browse files Browse the repository at this point in the history
1. Ditch wrapAsPluginError as we can simply use NewPluginError now.

2. Add a TODO to remove pluginError.Cause method.

3. Stop referring to pkg/errors method (except for 2 above).

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Oct 10, 2024
1 parent fa30284 commit 8eb1567
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
16 changes: 4 additions & 12 deletions cli-plugins/manager/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ func (e *pluginError) Error() string {
return e.cause.Error()
}

// Cause satisfies the errors.causer interface for pluginError.
// Cause satisfies the github.com/pkg/errors.causer interface for pluginError.
// TODO: remove this once all users switch away from github.com/pkg/errors.
func (e *pluginError) Cause() error {
return e.cause
}

// Unwrap provides compatibility for Go 1.13 error chains.
// Unwrap provides compatibility for Go 1.13+ error chains.
func (e *pluginError) Unwrap() error {
return e.cause
}
Expand All @@ -38,17 +39,8 @@ func (e *pluginError) MarshalText() (text []byte, err error) {
return []byte(e.cause.Error()), nil
}

// wrapAsPluginError wraps an error in a pluginError with an
// additional message, analogous to errors.Wrapf.
func wrapAsPluginError(err error, msg string) error {
if err == nil {
return nil
}
return &pluginError{cause: fmt.Errorf(msg+": %w", err)}
}

// NewPluginError creates a new pluginError, analogous to
// errors.Errorf.
// [fmt.Errorf].
func NewPluginError(msg string, args ...any) error {
return &pluginError{cause: fmt.Errorf(msg, args...)}
}
2 changes: 1 addition & 1 deletion cli-plugins/manager/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestPluginError(t *testing.T) {
assert.Check(t, is.Error(err, "new error"))

inner := errors.New("testing")
err = wrapAsPluginError(inner, "wrapping")
err = NewPluginError("wrapping: %w", inner)
assert.Check(t, is.Error(err, "wrapping: testing"))
assert.Check(t, is.ErrorIs(err, inner))

Expand Down
8 changes: 4 additions & 4 deletions cli-plugins/manager/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) {
// We are supposed to check for relevant execute permissions here. Instead we rely on an attempt to execute.
meta, err := c.Metadata()
if err != nil {
p.Err = wrapAsPluginError(err, "failed to fetch metadata")
p.Err = NewPluginError("failed to fetch metadata: %w", err)
return p, nil
}

if err := json.Unmarshal(meta, &p.Metadata); err != nil {
p.Err = wrapAsPluginError(err, "invalid metadata")
p.Err = NewPluginError("invalid metadata: %w", err)
return p, nil
}
if p.Metadata.SchemaVersion != "0.1.0" {
Expand All @@ -110,15 +110,15 @@ func newPlugin(c Candidate, cmds []*cobra.Command) (Plugin, error) {
func (p *Plugin) RunHook(ctx context.Context, hookData HookPluginData) ([]byte, error) {
hDataBytes, err := json.Marshal(hookData)
if err != nil {
return nil, wrapAsPluginError(err, "failed to marshall hook data")
return nil, NewPluginError("failed to marshall hook data: %w", err)
}

pCmd := exec.CommandContext(ctx, p.Path, p.Name, HookSubcommandName, string(hDataBytes))
pCmd.Env = os.Environ()
pCmd.Env = append(pCmd.Env, ReexecEnvvar+"="+os.Args[0])
hookCmdOutput, err := pCmd.Output()
if err != nil {
return nil, wrapAsPluginError(err, "failed to execute plugin hook subcommand")
return nil, NewPluginError("failed to execute plugin hook subcommand: %w", err)
}

return hookCmdOutput, nil
Expand Down

0 comments on commit 8eb1567

Please sign in to comment.