Skip to content

Commit 42a9f95

Browse files
author
Ali Beyad
authored
This commit changes the exception type thrown when trying to (#22921)
create a snapshot with a name that already exists in the repository. Instead of throwing a SnapshotCreateException, which results in a generic 500 status code, a duplicate snapshot name will throw a InvalidSnapshotNameException, which will result in a 400 status code (bad request).
1 parent eb26e1a commit 42a9f95

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import org.elasticsearch.repositories.RepositoryData;
9595
import org.elasticsearch.repositories.RepositoryException;
9696
import org.elasticsearch.repositories.RepositoryVerificationException;
97+
import org.elasticsearch.snapshots.InvalidSnapshotNameException;
9798
import org.elasticsearch.snapshots.SnapshotCreationException;
9899
import org.elasticsearch.snapshots.SnapshotException;
99100
import org.elasticsearch.snapshots.SnapshotId;
@@ -308,10 +309,10 @@ public void initializeSnapshot(SnapshotId snapshotId, List<IndexId> indices, Met
308309
// check if the snapshot name already exists in the repository
309310
final RepositoryData repositoryData = getRepositoryData();
310311
if (repositoryData.getAllSnapshotIds().stream().anyMatch(s -> s.getName().equals(snapshotName))) {
311-
throw new SnapshotCreationException(metadata.name(), snapshotId, "snapshot with the same name already exists");
312+
throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists");
312313
}
313314
if (snapshotFormat.exists(snapshotsBlobContainer, snapshotId.getUUID())) {
314-
throw new SnapshotCreationException(metadata.name(), snapshotId, "snapshot with such name already exists");
315+
throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists");
315316
}
316317

317318
// Write Global MetaData

core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.action.ListenableActionFuture;
2626
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;
2727
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
28-
import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse;
2928
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse;
3029
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
3130
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotIndexShardStage;
@@ -2450,7 +2449,7 @@ public void testCannotCreateSnapshotsWithSameName() throws Exception {
24502449
.get();
24512450
fail("should not be allowed to create a snapshot with the same name as an already existing snapshot: " +
24522451
createSnapshotResponse.getSnapshotInfo().snapshotId());
2453-
} catch (SnapshotCreationException e) {
2452+
} catch (InvalidSnapshotNameException e) {
24542453
assertThat(e.getMessage(), containsString("snapshot with the same name already exists"));
24552454
}
24562455

0 commit comments

Comments
 (0)