From 4788dd5b93b11ed5f49e2f07ea4f547ec62163d7 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 11 May 2020 17:56:58 +0200 Subject: [PATCH] Use common errors in context store Signed-off-by: Christopher Crone --- context/store/store.go | 7 ++++--- context/store/store_test.go | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/context/store/store.go b/context/store/store.go index b20400666..ea0dd6ca6 100644 --- a/context/store/store.go +++ b/context/store/store.go @@ -30,13 +30,14 @@ package store import ( "context" "encoding/json" - "fmt" "io/ioutil" "os" "path/filepath" "reflect" + "github.com/docker/api/errdefs" "github.com/opencontainers/go-digest" + "github.com/pkg/errors" ) const ( @@ -118,7 +119,7 @@ func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) { meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile) m, err := read(meta, getter) if os.IsNotExist(err) { - return nil, fmt.Errorf("unknown context %q", name) + return nil, errors.Wrapf(errdefs.ErrNotFound, "context %q", name) } else if err != nil { return nil, err } @@ -186,7 +187,7 @@ func (s *store) Create(name string, data TypedContext) error { dir := contextdirOf(name) metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir) if _, err := os.Stat(metaDir); !os.IsNotExist(err) { - return fmt.Errorf("context %q already exists", name) + return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", name) } err := os.Mkdir(metaDir, 0755) diff --git a/context/store/store_test.go b/context/store/store_test.go index c1dee9c65..7c8999fbb 100644 --- a/context/store/store_test.go +++ b/context/store/store_test.go @@ -33,6 +33,7 @@ import ( "os" "testing" + "github.com/docker/api/errdefs" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -62,12 +63,17 @@ func (suite *StoreTestSuite) AfterTest(suiteName, testName string) { func (suite *StoreTestSuite) TestCreate() { err := suite.store.Create("test", TypedContext{}) require.Nil(suite.T(), err) + + err = suite.store.Create("test", TypedContext{}) + require.EqualError(suite.T(), err, `context "test": already exists`) + require.True(suite.T(), errdefs.IsAlreadyExistsError(err)) } func (suite *StoreTestSuite) TestGetUnknown() { meta, err := suite.store.Get("unknown", nil) require.Nil(suite.T(), meta) - require.Error(suite.T(), err) + require.EqualError(suite.T(), err, `context "unknown": not found`) + require.True(suite.T(), errdefs.IsNotFoundError(err)) } func (suite *StoreTestSuite) TestGet() {