mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Use common errors in context store
Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
parent
b55f4b0547
commit
4788dd5b93
@ -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)
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user