use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class ModelProviderLocatorTest method locateProviderWithoutAccess.
/**
* When the ModelProviderLocator attempts to locate a ModelProvider whose getInstance() method
* is not accessible, the locator should raise an exception.
*/
@Test
public void locateProviderWithoutAccess() {
String className = Objects.requireNonNull(BadAccessModelProvider.class.getName());
DataStoreException actualException = assertThrows(DataStoreException.class, () -> ModelProviderLocator.locate(className));
assertEquals("Tried to call " + BadAccessModelProvider.class.getName() + "getInstance" + ", but this method did not have public access.", actualException.getMessage());
assertEquals("Validate that " + BadAccessModelProvider.class.getName() + " has not been modified since the time it was code-generated.", actualException.getRecoverySuggestion());
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class ModelProviderLocatorTest method locateNotAModelProvider.
/**
* When the ModelProviderLocator attempts to call locate a class that *is not* an
* {@link ModelProvider}, the locator should bubble up that exception as DataStoreException.
*/
@Test
public void locateNotAModelProvider() {
String className = Objects.requireNonNull(NotAModelProvider.class.getName());
DataStoreException actualException = assertThrows(DataStoreException.class, () -> ModelProviderLocator.locate(className));
assertEquals("Located class as " + NotAModelProvider.class.getName() + ", but it does not implement com.amplifyframework.core.model.ModelProvider.", actualException.getMessage());
assertEquals("Validate that " + NotAModelProvider.class.getName() + " has not been modified since the time it was code-generated.", actualException.getRecoverySuggestion());
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class VersionRepositoryTest method emitsErrorWhenMetadataHasNullVersion.
/**
* When you try to get the version for a model, and there is metadata for the model
* in the DataStore, BUT the version info is not populated, this should return an
* {@link DataStoreException}.
* @throws DataStoreException
* NOT EXPECTED. This happens on failure to arrange data before test action.
* The expected DataStoreException is communicated via callback, not thrown
* on the calling thread. It's a different thing than this.
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
*/
@Test
public void emitsErrorWhenMetadataHasNullVersion() throws DataStoreException, InterruptedException {
// Arrange a model an metadata into the store, but the metadtaa doesn't contain a valid version
BlogOwner blogOwner = BlogOwner.builder().name("Jameson").build();
ModelMetadata metadata = new ModelMetadata(blogOwner.getModelName() + "|" + blogOwner.getId(), null, null, null);
storageAdapter.save(blogOwner, metadata);
// Act: try to get the version.
TestObserver<Integer> observer = versionRepository.findModelVersion(blogOwner).test();
assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
// Assert: the single emitted a DataStoreException.
observer.assertError(error -> {
if (!(error instanceof DataStoreException)) {
return false;
}
String expectedMessage = String.format(Locale.US, "Metadata for item with id = %s had null version.", blogOwner.getId());
return expectedMessage.equals(error.getMessage());
});
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class AppSyncClient method mutation.
private <T extends Model> Cancelable mutation(final GraphQLRequest<ModelWithMetadata<T>> request, final Consumer<GraphQLResponse<ModelWithMetadata<T>>> onResponse, final Consumer<DataStoreException> onFailure) {
final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
if (response.hasErrors()) {
onResponse.accept(new GraphQLResponse<>(null, response.getErrors()));
} else {
onResponse.accept(response);
}
};
final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure during mutation.", failure, "Check details."));
final Cancelable cancelable = api.mutate(request, responseConsumer, failureConsumer);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class AppSyncClient method sync.
@NonNull
@Override
public <T extends Model> Cancelable sync(@NonNull GraphQLRequest<PaginatedResult<ModelWithMetadata<T>>> request, @NonNull Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> onResponse, @NonNull Consumer<DataStoreException> onFailure) {
final Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> responseConsumer = apiQueryResponse -> {
if (apiQueryResponse.hasErrors()) {
onFailure.accept(new DataStoreException("Failure performing sync query to AppSync: " + apiQueryResponse.getErrors().toString(), AmplifyException.TODO_RECOVERY_SUGGESTION));
} else {
onResponse.accept(apiQueryResponse);
}
};
final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure performing sync query to AppSync.", failure, AmplifyException.TODO_RECOVERY_SUGGESTION));
final Cancelable cancelable = api.query(request, responseConsumer, failureConsumer);
if (cancelable != null) {
return cancelable;
}
return new NoOpCancelable();
}
Aggregations