Skip to content

Commit

Permalink
updated error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Nov 24, 2023
1 parent aa458a8 commit 59101ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
49 changes: 49 additions & 0 deletions plugin/errors.go
Original file line number Diff line number Diff line change
@@ -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"
}
19 changes: 2 additions & 17 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)}
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand All @@ -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)
}
})

Expand Down

0 comments on commit 59101ed

Please sign in to comment.