From 690448ee6711105690637990f499c6d4a545fa0d Mon Sep 17 00:00:00 2001 From: Junjie Gao Date: Thu, 18 Jan 2024 21:19:49 +0800 Subject: [PATCH] fix: update PluginExecutableFileError type (#375) Signed-off-by: Junjie Gao --------- Signed-off-by: Junjie Gao --- plugin/errors.go | 18 +++++++++++++++++- plugin/manager.go | 2 +- plugin/plugin.go | 8 ++++++-- plugin/plugin_test.go | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/plugin/errors.go b/plugin/errors.go index ab7394d0..38e56692 100644 --- a/plugin/errors.go +++ b/plugin/errors.go @@ -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 +} diff --git a/plugin/manager.go b/plugin/manager.go index ffb80ca3..67a24395 100644 --- a/plugin/manager.go +++ b/plugin/manager.go @@ -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 { diff --git a/plugin/plugin.go b/plugin/plugin.go index be74b097..94befe84 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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) @@ -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, } } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index a6f49a3b..4a797fe4 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -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{})