Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update PluginExecutableFileError type #375

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading