Skip to content

Commit b27bb99

Browse files
committed
fix: change error message from hash to path already exists
This error was returned when an object was already stored at a given path and had a different hash to the object being put in. It said hash already exists which was wrong. Now it reports that the path already exists with a different hash.
1 parent 06038f5 commit b27bb99

File tree

8 files changed

+9
-9
lines changed

8 files changed

+9
-9
lines changed

domain/objectstore/errors/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ const (
1414
// there is a collision in the hash function.
1515
ErrHashAndSizeAlreadyExists = errors.ConstError("hash exists for different file size")
1616

17-
// ErrHashAlreadyExists is returned when a hash already exists.
18-
ErrHashAlreadyExists = errors.ConstError("hash already exists")
17+
// ErrPathAlreadyExistsDifferentHash is returned when a hash already exists.
18+
ErrPathAlreadyExistsDifferentHash = errors.ConstError("path already exists with different hash")
1919
)

domain/objectstore/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ AND size = $dbMetadata.size`, dbMetadata, dbMetadataPath)
163163

164164
err = tx.Query(ctx, pathStmt, dbMetadataPath).Get(&outcome)
165165
if database.IsErrConstraintPrimaryKey(err) {
166-
return objectstoreerrors.ErrHashAlreadyExists
166+
return objectstoreerrors.ErrPathAlreadyExistsDifferentHash
167167
} else if err != nil {
168168
return errors.Annotatef(err, "inserting metadata path")
169169
}

domain/objectstore/state/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (s *stateSuite) TestPutMetadataConflict(c *gc.C) {
102102

103103
_, err = st.PutMetadata(context.Background(), metadata)
104104
c.Assert(err, gc.Not(jc.ErrorIsNil))
105-
c.Check(err, jc.ErrorIs, objectstoreerrors.ErrHashAlreadyExists)
105+
c.Check(err, jc.ErrorIs, objectstoreerrors.ErrPathAlreadyExistsDifferentHash)
106106
}
107107

108108
func (s *stateSuite) TestPutMetadataWithSameHashAndSize(c *gc.C) {

internal/bootstrap/agentbinary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func PopulateAgentBinary(ctx context.Context, dataDir string, storage AgentBinar
6565
logger.Debugf("Adding agent binary: %v", agentTools.Version)
6666

6767
// If the hash already exists, we don't need to add it again.
68-
if err := storage.Add(ctx, bytes.NewReader(data), metadata); err != nil && !errors.Is(err, objectstoreerrors.ErrHashAlreadyExists) {
68+
if err := storage.Add(ctx, bytes.NewReader(data), metadata); err != nil && !errors.Is(err, objectstoreerrors.ErrPathAlreadyExistsDifferentHash) {
6969
return nil, errors.Trace(err)
7070
}
7171

internal/bootstrap/agentbinary_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *agentBinarySuite) TestPopulateAgentBinaryTwiceShouldSucceed(c *gc.C) {
8888
Version: current.String(),
8989
Size: size,
9090
SHA256: "sha256",
91-
}).Return(objectstoreerrors.ErrHashAlreadyExists)
91+
}).Return(objectstoreerrors.ErrPathAlreadyExistsDifferentHash)
9292

9393
cleanup, err := PopulateAgentBinary(context.Background(), dir, s.storage, s.logger)
9494
c.Assert(err, jc.ErrorIsNil)

internal/charm/services/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *CharmStorage) Store(ctx context.Context, charmURL string, downloadedCha
7272
}
7373

7474
// If the blob is already stored, we can skip the upload.
75-
if _, err := s.objectStore.Put(ctx, storagePath, downloadedCharm.CharmData, downloadedCharm.Size); err != nil && !errors.Is(err, objectstoreerrors.ErrHashAlreadyExists) {
75+
if _, err := s.objectStore.Put(ctx, storagePath, downloadedCharm.CharmData, downloadedCharm.Size); err != nil && !errors.Is(err, objectstoreerrors.ErrPathAlreadyExistsDifferentHash) {
7676
return "", errors.Annotate(err, "cannot add charm to storage")
7777
}
7878

internal/charm/services/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *storageTestSuite) TestStoreBlobAlreadyStored(c *gc.C) {
9191
CharmVersion: "the-version",
9292
}
9393

94-
s.storageBackend.EXPECT().Put(gomock.Any(), expStoreCharmPath, gomock.AssignableToTypeOf(dlCharm.CharmData), int64(7337)).Return("", objectstoreerrors.ErrHashAlreadyExists)
94+
s.storageBackend.EXPECT().Put(gomock.Any(), expStoreCharmPath, gomock.AssignableToTypeOf(dlCharm.CharmData), int64(7337)).Return("", objectstoreerrors.ErrPathAlreadyExistsDifferentHash)
9595
s.stateBackend.EXPECT().UpdateUploadedCharm(state.CharmInfo{
9696
StoragePath: expStoreCharmPath,
9797
ID: curl,

state/binarystorage/binarystorage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func New(
6565
func (s *binaryStorage) Add(ctx context.Context, r io.Reader, metadata Metadata) (resultErr error) {
6666
// Add the binary file to storage.
6767
path := fmt.Sprintf("tools/%s-%s", metadata.Version, metadata.SHA256)
68-
if _, err := s.managedStorage.Put(ctx, path, r, metadata.Size); err != nil && !errors.Is(err, objectstoreerrors.ErrHashAlreadyExists) {
68+
if _, err := s.managedStorage.Put(ctx, path, r, metadata.Size); err != nil && !errors.Is(err, objectstoreerrors.ErrPathAlreadyExistsDifferentHash) {
6969
return errors.Annotate(err, "cannot store binary file")
7070
}
7171
defer func() {

0 commit comments

Comments
 (0)