Skip to content

Commit

Permalink
fix: update PluginExecutableFileError type (notaryproject#375)
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>

---------

Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao authored Jan 18, 2024
1 parent d52ca71 commit 690448e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
18 changes: 17 additions & 1 deletion plugin/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,20 @@ type PluginDirectoryWalkError error
// PluginExecutableFileError is used when there is an issue with plugin
// executable file and should suggest user to check the existence, permission
// and platform/arch compatibility of plugin.
type PluginExecutableFileError error
type PluginExecutableFileError struct {
Msg string
InnerError error
}

// Error returns the error message.
func (e PluginExecutableFileError) Error() string {
if e.Msg != "" {
return e.Msg
}
return e.InnerError.Error()
}

// Unwrap returns the inner error.
func (e PluginExecutableFileError) Unwrap() error {
return e.InnerError
}
2 changes: 1 addition & 1 deletion plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions)
if installOpts.PluginPath == "" {
return nil, nil, errors.New("plugin source path cannot be empty")
}
logger.Debugf("Installing plugin from plugin path %s", installOpts.PluginPath)
logger.Debugf("Installing plugin from path %s", installOpts.PluginPath)
var installFromNonDir bool
pluginExecutableFile, pluginName, err := parsePluginFromDir(ctx, installOpts.PluginPath)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func run(ctx context.Context, pluginName string, pluginPath string, req proto.Re
if len(stderr) == 0 {
// if stderr is empty, it is possible that the plugin is not
// running properly.
return PluginExecutableFileError(fmt.Errorf("failed to execute the %s command for plugin %s: %w", req.Command(), pluginName, err))
return &PluginExecutableFileError{
Msg: fmt.Sprintf("failed to execute the %s command for plugin %s", req.Command(), pluginName),
InnerError: err,
}
} else {
var re proto.RequestError
jsonErr := json.Unmarshal(stderr, &re)
Expand All @@ -205,8 +208,9 @@ func run(ctx context.Context, pluginName string, pluginPath string, req proto.Re
logger.Debugf("Plugin %s response: %s", req.Command(), string(stdout))
// deserialize response
if err = json.Unmarshal(stdout, resp); err != nil {
logger.Errorf("failed to unmarshal plugin %s response: %w", req.Command(), err)
return &PluginMalformedError{
Msg: fmt.Sprintf("plugin %s response for %s command isn't compliant with Notation plugin requirement: %s", pluginName, req.Command(), string(stdout)),
Msg: fmt.Sprintf("failed to unmarshal the response of %s command for plugin %s", req.Command(), pluginName),
InnerError: err,
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestGetMetadata(t *testing.T) {
t.Run("plugin cause system error", func(t *testing.T) {
exitErr := errors.New("system error")
stderr := []byte("")
expectedErrMsg := "failed to execute the get-plugin-metadata command for plugin test-plugin: system error"
expectedErrMsg := "failed to execute the get-plugin-metadata command for plugin test-plugin"
plugin := CLIPlugin{name: "test-plugin"}
executor = testCommander{stdout: nil, stderr: stderr, err: exitErr}
_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
Expand Down

0 comments on commit 690448e

Please sign in to comment.