From 42a9f95fde016a850ab11c4fd9986030d6cf5b8b Mon Sep 17 00:00:00 2001 From: Ali Beyad Date: Mon, 6 Feb 2017 11:39:59 -0600 Subject: [PATCH] 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). --- .../repositories/blobstore/BlobStoreRepository.java | 5 +++-- .../snapshots/SharedClusterSnapshotRestoreIT.java | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java b/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java index ab4b63e0781..bcf288dab27 100644 --- a/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java +++ b/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java @@ -94,6 +94,7 @@ import org.elasticsearch.repositories.Repository; import org.elasticsearch.repositories.RepositoryData; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.RepositoryVerificationException; +import org.elasticsearch.snapshots.InvalidSnapshotNameException; import org.elasticsearch.snapshots.SnapshotCreationException; import org.elasticsearch.snapshots.SnapshotException; import org.elasticsearch.snapshots.SnapshotId; @@ -308,10 +309,10 @@ public abstract class BlobStoreRepository extends AbstractLifecycleComponent imp // check if the snapshot name already exists in the repository final RepositoryData repositoryData = getRepositoryData(); if (repositoryData.getAllSnapshotIds().stream().anyMatch(s -> s.getName().equals(snapshotName))) { - throw new SnapshotCreationException(metadata.name(), snapshotId, "snapshot with the same name already exists"); + throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists"); } if (snapshotFormat.exists(snapshotsBlobContainer, snapshotId.getUUID())) { - throw new SnapshotCreationException(metadata.name(), snapshotId, "snapshot with such name already exists"); + throw new InvalidSnapshotNameException(metadata.name(), snapshotId.getName(), "snapshot with the same name already exists"); } // Write Global MetaData diff --git a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index 6116cfa8b98..aa229b0910c 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -25,7 +25,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; -import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotIndexShardStage; @@ -2450,7 +2449,7 @@ public class SharedClusterSnapshotRestoreIT extends AbstractSnapshotIntegTestCas .get(); fail("should not be allowed to create a snapshot with the same name as an already existing snapshot: " + createSnapshotResponse.getSnapshotInfo().snapshotId()); - } catch (SnapshotCreationException e) { + } catch (InvalidSnapshotNameException e) { assertThat(e.getMessage(), containsString("snapshot with the same name already exists")); }