Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: specialize not-found and already-exists exceptions #362

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions momento-sdk/src/intTest/java/momento/sdk/CacheClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import momento.sdk.auth.StringCredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.exceptions.AuthenticationException;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.exceptions.InvalidArgumentException;
import momento.sdk.exceptions.NotFoundException;
import momento.sdk.exceptions.ServerUnavailableException;
import momento.sdk.responses.cache.DeleteResponse;
import momento.sdk.responses.cache.GetBatchResponse;
Expand Down Expand Up @@ -148,7 +148,7 @@ public void shouldReturnNotFoundWhenCacheToFlushDoesNotExist() {
assertThat(target.flushCache(randomString("name")))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CacheFlushResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -643,7 +643,7 @@ public void getBatchFailsWithNonExistentCache() {
assertThat(target.getBatch(randomString("cache"), items))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(GetBatchResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand All @@ -662,7 +662,7 @@ public void setBatchFailsWithNonExistentCache() {
assertThat(target.setBatch(randomString("cache"), items))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SetBatchResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -702,6 +702,6 @@ public void setBatchStringBytessFailsWithNonExistentCache() {
assertThat(target.setBatchStringBytes(randomString("cache"), items))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SetBatchResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import java.time.Duration;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.exceptions.AlreadyExistsException;
import momento.sdk.exceptions.AuthenticationException;
import momento.sdk.exceptions.BadRequestException;
import momento.sdk.exceptions.CacheAlreadyExistsException;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.exceptions.InvalidArgumentException;
import momento.sdk.exceptions.NotFoundException;
import momento.sdk.responses.cache.control.CacheCreateResponse;
import momento.sdk.responses.cache.control.CacheDeleteResponse;
import momento.sdk.responses.cache.control.CacheListResponse;
Expand Down Expand Up @@ -74,15 +74,16 @@ public void throwsAlreadyExistsWhenCreatingExistingCache() {
assertThat(target.createCache(existingCache))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CacheCreateResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(AlreadyExistsException.class));
.satisfies(
error -> assertThat(error).hasCauseInstanceOf(CacheAlreadyExistsException.class));
}

@Test
public void returnsNotFoundWhenDeletingUnknownCache() {
assertThat(target.deleteCache(randomString("name")))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CacheDeleteResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -140,7 +141,8 @@ public void deleteSucceeds() {
assertThat(target.createCache(cacheName))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CacheCreateResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(AlreadyExistsException.class));
.satisfies(
error -> assertThat(error).hasCauseInstanceOf(CacheAlreadyExistsException.class));

assertThat(target.deleteCache(cacheName))
.succeedsWithin(FIVE_SECONDS)
Expand All @@ -149,7 +151,7 @@ public void deleteSucceeds() {
assertThat(target.deleteCache(cacheName))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(CacheDeleteResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import momento.sdk.auth.StringCredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.exceptions.AuthenticationException;
import momento.sdk.exceptions.NotFoundException;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.exceptions.TimeoutException;
import momento.sdk.responses.cache.DeleteResponse;
import momento.sdk.responses.cache.GetResponse;
Expand Down Expand Up @@ -101,11 +101,11 @@ public void nonExistentCacheNameReturnsErrorOnGetOrSet() {

final GetResponse getResponse = client.get(cacheName, "").join();
assertThat(getResponse).isInstanceOf(GetResponse.Error.class);
assertThat(((GetResponse.Error) getResponse)).hasCauseInstanceOf(NotFoundException.class);
assertThat(((GetResponse.Error) getResponse)).hasCauseInstanceOf(CacheNotFoundException.class);

final SetResponse setResponse = client.set(cacheName, "", "", Duration.ofSeconds(10)).join();
assertThat(setResponse).isInstanceOf(SetResponse.Error.class);
assertThat(((SetResponse.Error) setResponse)).hasCauseInstanceOf(NotFoundException.class);
assertThat(((SetResponse.Error) setResponse)).hasCauseInstanceOf(CacheNotFoundException.class);
}

@Test
Expand Down
38 changes: 19 additions & 19 deletions momento-sdk/src/intTest/java/momento/sdk/SortedSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.Map;
import java.util.Set;
import momento.sdk.config.Configurations;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.exceptions.InvalidArgumentException;
import momento.sdk.exceptions.NotFoundException;
import momento.sdk.requests.CollectionTtl;
import momento.sdk.responses.SortOrder;
import momento.sdk.responses.cache.sortedset.ScoredElement;
Expand Down Expand Up @@ -166,14 +166,14 @@ public void sortedSetPutElementReturnsErrorWithNonexistentCacheName() {
assertThat(client.sortedSetPutElement(randomString("cache"), sortedSetName, "element", 1.0))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetPutElementResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetPutElement(
randomString("cache"), sortedSetName, "element".getBytes(), 1.0))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetPutElementResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -279,7 +279,7 @@ public void sortedSetPutElementsReturnsErrorWithNonexistentCacheName() {
randomString("cache"), sortedSetName, Collections.singletonMap("element", 1.0)))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetPutElementsResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetPutElementsByteArray(
Expand All @@ -288,7 +288,7 @@ public void sortedSetPutElementsReturnsErrorWithNonexistentCacheName() {
Collections.singletonMap("element".getBytes(), 1.0)))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetPutElementsResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -434,7 +434,7 @@ public void sortedSetFetchByRankReturnsErrorWithNonexistentCacheName() {
assertThat(client.sortedSetFetchByRank(randomString("cache"), sortedSetName))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetFetchResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -558,7 +558,7 @@ public void sortedSetFetchByScoreReturnsErrorWithNonexistentCacheName() {
randomString("cache"), sortedSetName, null, null, null, 0, 100))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetFetchResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -627,14 +627,14 @@ public void sortedSetGetRankReturnsErrorWithNonexistentCacheName() {
randomString("cache"), sortedSetName, "element", SortOrder.ASCENDING))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetRankResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetGetRank(
randomString("cache"), sortedSetName, "element".getBytes(), SortOrder.ASCENDING))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetRankResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -743,12 +743,12 @@ public void sortedSetGetScoreReturnsErrorWithNonexistentCacheName() {
assertThat(client.sortedSetGetScore(randomString("cache"), sortedSetName, "element"))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetScoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(client.sortedSetGetScore(randomString("cache"), sortedSetName, "element".getBytes()))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetScoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -848,14 +848,14 @@ public void sortedSetGetScoresReturnsErrorWithNonexistentCacheName() {
randomString("cache"), sortedSetName, Collections.singleton("element")))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetScoresResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetGetScoresByteArray(
randomString("cache"), sortedSetName, Collections.singleton("element".getBytes())))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetGetScoresResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -986,14 +986,14 @@ public void sortedSetIncrementScoreReturnsErrorWithNonexistentCacheName() {
assertThat(client.sortedSetIncrementScore(randomString("cache"), sortedSetName, "element", 1.0))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetIncrementScoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetIncrementScore(
randomString("cache"), sortedSetName, "element".getBytes(), 1.0))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetIncrementScoreResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -1106,14 +1106,14 @@ public void sortedSetRemoveElementReturnsErrorWithNonexistentCacheName() {
assertThat(client.sortedSetRemoveElement(randomString("cache"), sortedSetName, "element"))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetRemoveElementResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetRemoveElement(
randomString("cache"), sortedSetName, "element".getBytes()))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetRemoveElementResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down Expand Up @@ -1226,14 +1226,14 @@ public void sortedSetRemoveElementsReturnsErrorWithNonexistentCacheName() {
randomString("cache"), sortedSetName, Collections.emptySet()))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetRemoveElementsResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));

assertThat(
client.sortedSetRemoveElementsByteArray(
randomString("cache"), sortedSetName, Collections.emptySet()))
.succeedsWithin(FIVE_SECONDS)
.asInstanceOf(InstanceOfAssertFactories.type(SortedSetRemoveElementsResponse.Error.class))
.satisfies(error -> assertThat(error).hasCauseInstanceOf(NotFoundException.class));
.satisfies(error -> assertThat(error).hasCauseInstanceOf(CacheNotFoundException.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import momento.sdk.PreviewStorageClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.StorageConfigurations;
import momento.sdk.exceptions.NotFoundException;
import momento.sdk.exceptions.CacheNotFoundException;
import momento.sdk.responses.storage.DeleteResponse;
import momento.sdk.responses.storage.GetResponse;
import momento.sdk.responses.storage.PutResponse;
Expand Down Expand Up @@ -112,11 +112,11 @@ public void badStoreNameReturnsError() {

final GetResponse getResponse = client.get(storeName, "").join();
assertThat(getResponse).isInstanceOf(GetResponse.Error.class);
assertThat(((GetResponse.Error) getResponse)).hasCauseInstanceOf(NotFoundException.class);
assertThat(((GetResponse.Error) getResponse)).hasCauseInstanceOf(CacheNotFoundException.class);

final PutResponse putResponse = client.put(storeName, "", "").join();
assertThat(putResponse).isInstanceOf(PutResponse.Error.class);
assertThat(((PutResponse.Error) putResponse)).hasCauseInstanceOf(NotFoundException.class);
assertThat(((PutResponse.Error) putResponse)).hasCauseInstanceOf(CacheNotFoundException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import javax.annotation.Nonnull;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.StorageConfiguration;
import momento.sdk.exceptions.AlreadyExistsException;
import momento.sdk.exceptions.CacheServiceExceptionMapper;
import momento.sdk.exceptions.SdkException;
import momento.sdk.exceptions.StoreAlreadyExistsException;
import momento.sdk.responses.storage.CreateStoreResponse;
import momento.sdk.responses.storage.DeleteStoreResponse;
import momento.sdk.responses.storage.ListStoresResponse;
Expand Down Expand Up @@ -81,7 +81,7 @@ private CompletableFuture<CreateStoreResponse> sendCreateStore(String storeName)
final Function<Throwable, CreateStoreResponse> failure =
e -> {
final SdkException sdkException = CacheServiceExceptionMapper.convert(e);
if (sdkException instanceof AlreadyExistsException) {
if (sdkException instanceof StoreAlreadyExistsException) {
return new CreateStoreResponse.AlreadyExists();
}
return new CreateStoreResponse.Error(CacheServiceExceptionMapper.convert(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import momento.sdk.internal.MomentoTransportErrorDetails;

/** A resource already exists. */
public class AlreadyExistsException extends MomentoServiceException {

private static final String MESSAGE =
"A cache with the specified name already exists. To resolve this error, "
+ "either delete the existing cache and make a new one, or use a different name.";
@Deprecated // Use CacheAlreadyExistsException instead
public class AlreadyExistsException extends CacheAlreadyExistsException {

/**
* Constructs an AlreadyExistsException with a cause and error details.
Expand All @@ -17,18 +14,6 @@ public class AlreadyExistsException extends MomentoServiceException {
*/
public AlreadyExistsException(
Throwable cause, MomentoTransportErrorDetails transportErrorDetails) {
super(
MomentoErrorCode.ALREADY_EXISTS_ERROR,
completeMessage(transportErrorDetails),
cause,
transportErrorDetails);
}

private static String completeMessage(MomentoTransportErrorDetails transportErrorDetails) {
return transportErrorDetails
.getGrpcErrorDetails()
.getCacheName()
.map(s -> MESSAGE + " Cache name: " + s)
.orElse(MESSAGE);
super(cause, transportErrorDetails);
}
}
Loading
Loading