From 59101ed01bf09d93abea74c322aeb0797c12451d Mon Sep 17 00:00:00 2001 From: Patrick Zheng Date: Fri, 24 Nov 2023 17:33:35 +0800 Subject: [PATCH] updated error messages Signed-off-by: Patrick Zheng --- plugin/errors.go | 49 ++++++++++++++++++++++++++++++++++++++++++ plugin/manager.go | 19 ++-------------- plugin/manager_test.go | 10 +++++---- 3 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 plugin/errors.go diff --git a/plugin/errors.go b/plugin/errors.go new file mode 100644 index 00000000..248a07b7 --- /dev/null +++ b/plugin/errors.go @@ -0,0 +1,49 @@ +// Copyright The Notary Project Authors. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package plugin + +import "errors" + +// ErrNotCompliant is returned by plugin methods when the response is not +// compliant. +var ErrNotCompliant = errors.New("plugin not compliant") + +// ErrNotRegularFile is returned when the plugin file is not an regular file. +var ErrNotRegularFile = errors.New("not regular file") + +// ErrInstallLowerVersion is returned when installing a plugin with version +// lower than the exisiting plugin version. +type ErrInstallLowerVersion struct { + Msg string +} + +func (e ErrInstallLowerVersion) Error() string { + if e.Msg != "" { + return e.Msg + } + return "installing plugin with version lower than the existing plugin version" +} + +// ErrInstallEqualVersion is returned when installing a plugin with version +// equal to the exisiting plugin version. +type ErrInstallEqualVersion struct { + Msg string +} + +func (e ErrInstallEqualVersion) Error() string { + if e.Msg != "" { + return e.Msg + } + return "installing plugin with version equal to the existing plugin version" +} diff --git a/plugin/manager.go b/plugin/manager.go index 76fb7485..dabc9d79 100644 --- a/plugin/manager.go +++ b/plugin/manager.go @@ -28,21 +28,6 @@ import ( "github.com/notaryproject/notation-go/plugin/proto" ) -// ErrNotCompliant is returned by plugin methods when the response is not -// compliant. -var ErrNotCompliant = errors.New("plugin not compliant") - -// ErrNotRegularFile is returned when the plugin file is not an regular file. -var ErrNotRegularFile = errors.New("not regular file") - -// ErrInstallLowerVersion is returned when installing a plugin with version -// lower than the exisiting plugin version. -var ErrInstallLowerVersion = errors.New("installing plugin with version lower than the existing plugin version") - -// ErrInstallEqualVersion is returned when installing a plugin with version -// equal to the exisiting plugin version. -var ErrInstallEqualVersion = errors.New("installing plugin with version equal to the existing plugin version") - // Manager manages plugins installed on the system. type Manager interface { Get(ctx context.Context, name string) (Plugin, error) @@ -146,9 +131,9 @@ func (m *CLIManager) Install(ctx context.Context, filePath string, overwrite boo } switch { case comp < 0: - return nil, nil, ErrInstallLowerVersion + return nil, nil, ErrInstallLowerVersion{Msg: fmt.Sprintf("The installing plugin version %s is lower than the existing plugin version %s.", newPluginMetadata.Version, existingPluginMetadata.Version)} case comp == 0: - return nil, nil, ErrInstallEqualVersion + return nil, nil, ErrInstallEqualVersion{Msg: fmt.Sprintf("Plugin %s with version %s already exists.", pluginName, existingPluginMetadata.Version)} } } } diff --git a/plugin/manager_test.go b/plugin/manager_test.go index 3fe49acb..6f2cdb8b 100644 --- a/plugin/manager_test.go +++ b/plugin/manager_test.go @@ -221,8 +221,9 @@ func TestManager_Install(t *testing.T) { newPluginStdout: metadataJSON(validMetadata), } _, _, err = mgr.Install(context.Background(), newPluginFilePath, false) - if err == nil || err.Error() != ErrInstallEqualVersion.Error() { - t.Fatalf("expecting error %v, but got %v", ErrInstallEqualVersion, err) + expectedErrorMsg := "Plugin foo with version 1.0.0 already exists." + if err == nil || err.Error() != expectedErrorMsg { + t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err) } }) @@ -234,8 +235,9 @@ func TestManager_Install(t *testing.T) { newPluginStdout: metadataJSON(validMetadataLowerVersion), } _, _, err = mgr.Install(context.Background(), newPluginFilePath, false) - if err == nil || err.Error() != ErrInstallLowerVersion.Error() { - t.Fatalf("expecting error %v, but got %v", ErrInstallLowerVersion, err) + expectedErrorMsg := "The installing plugin version 0.1.0 is lower than the existing plugin version 1.0.0." + if err == nil || err.Error() != expectedErrorMsg { + t.Fatalf("expecting error %s, but got %v", expectedErrorMsg, err) } })