use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomObjectSync method handleError.
/**
* Given a {@link String} {@code errorMessage} and a {@link Throwable} {@code exception}, this
* method calls the optional error callback specified in the {@code syncOptions} and updates the
* {@code statistics} instance by incrementing the total number of failed custom objects to sync.
*
* @param errorMessage The error message describing the reason(s) of failure.
* @param exception The exception that called caused the failure, if any.
* @param failedTimes The number of times that the failed custom objects counter is incremented.
*/
private void handleError(@Nonnull final String errorMessage, @Nonnull final Throwable exception, final int failedTimes) {
SyncException syncException = new SyncException(errorMessage, exception);
syncOptions.applyErrorCallback(syncException);
statistics.incrementFailed(failedTimes);
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class ProductSync method resolveNowReadyReferences.
@Nonnull
private CompletionStage<Void> resolveNowReadyReferences(final Map<String, String> keyToIdCache) {
// We delete anyways the keys from the statistics before we attempt resolution, because even if
// resolution fails
// the products that failed to be synced would be counted as failed.
final Set<String> referencingDraftKeys = readyToResolve.stream().map(statistics::removeAndGetReferencingKeys).filter(Objects::nonNull).flatMap(Set::stream).collect(Collectors.toSet());
if (referencingDraftKeys.isEmpty()) {
return CompletableFuture.completedFuture(null);
}
final Set<ProductDraft> readyToSync = new HashSet<>();
final Set<WaitingToBeResolvedProducts> waitingDraftsToBeUpdated = new HashSet<>();
return unresolvedReferencesService.fetch(referencingDraftKeys, CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
final Set<WaitingToBeResolvedProducts> waitingDrafts = fetchResponse.getKey();
final Throwable fetchException = fetchResponse.getValue();
if (fetchException != null) {
final String errorMessage = format(UNRESOLVED_REFERENCES_STORE_FETCH_FAILED, referencingDraftKeys);
handleError(new SyncException(errorMessage, fetchException), referencingDraftKeys.size());
return CompletableFuture.completedFuture(null);
}
waitingDrafts.forEach(waitingDraft -> {
final Set<String> missingReferencedProductKeys = waitingDraft.getMissingReferencedProductKeys();
missingReferencedProductKeys.removeAll(readyToResolve);
if (missingReferencedProductKeys.isEmpty()) {
readyToSync.add(waitingDraft.getProductDraft());
} else {
waitingDraftsToBeUpdated.add(waitingDraft);
}
});
return updateWaitingDrafts(waitingDraftsToBeUpdated).thenCompose(aVoid -> syncBatch(readyToSync, keyToIdCache)).thenCompose(aVoid -> removeFromWaiting(readyToSync));
});
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class ProductSync method syncDraft.
@Nonnull
private CompletionStage<Void> syncDraft(@Nonnull final Set<ProductProjection> oldProducts, @Nonnull final ProductDraft newProductDraft) {
final Map<String, ProductProjection> oldProductMap = oldProducts.stream().collect(toMap(ProductProjection::getKey, identity()));
return productReferenceResolver.resolveReferences(newProductDraft).thenCompose(resolvedDraft -> {
final ProductProjection oldProduct = oldProductMap.get(newProductDraft.getKey());
return ofNullable(oldProduct).map(ProductProjection -> fetchProductAttributesMetadataAndUpdate(oldProduct, resolvedDraft)).orElseGet(() -> applyCallbackAndCreate(resolvedDraft));
}).exceptionally(completionException -> {
final String errorMessage = format(FAILED_TO_PROCESS, newProductDraft.getKey(), completionException.getMessage());
handleError(new SyncException(errorMessage, completionException), 1);
return null;
});
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomerSync method syncBatch.
@Nonnull
private CompletionStage<Void> syncBatch(@Nonnull final Set<Customer> oldCustomers, @Nonnull final Set<CustomerDraft> newCustomerDrafts) {
final Map<String, Customer> oldCustomerMap = oldCustomers.stream().collect(toMap(Customer::getKey, identity()));
return CompletableFuture.allOf(newCustomerDrafts.stream().map(customerDraft -> referenceResolver.resolveReferences(customerDraft).thenCompose(resolvedCustomerDraft -> syncDraft(oldCustomerMap, resolvedCustomerDraft)).exceptionally(completionException -> {
final String errorMessage = format(FAILED_TO_PROCESS, customerDraft.getKey(), completionException.getMessage());
handleError(new SyncException(errorMessage, completionException), 1);
return null;
})).map(CompletionStage::toCompletableFuture).toArray(CompletableFuture[]::new));
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomerSync method fetchAndUpdate.
@Nonnull
private CompletionStage<Void> fetchAndUpdate(@Nonnull final Customer oldCustomer, @Nonnull final CustomerDraft newCustomerDraft) {
final String customerKey = oldCustomer.getKey();
return customerService.fetchCustomerByKey(customerKey).handle(ImmutablePair::of).thenCompose(fetchResponse -> {
final Optional<Customer> fetchedCustomerOptional = fetchResponse.getKey();
final Throwable exception = fetchResponse.getValue();
if (exception != null) {
final String errorMessage = format(CTP_CUSTOMER_UPDATE_FAILED, customerKey, "Failed to fetch from CTP while retrying after concurrency modification.");
handleError(new SyncException(errorMessage, exception), 1);
return CompletableFuture.completedFuture(null);
}
return fetchedCustomerOptional.map(fetchedCustomer -> buildActionsAndUpdate(fetchedCustomer, newCustomerDraft)).orElseGet(() -> {
final String errorMessage = format(CTP_CUSTOMER_UPDATE_FAILED, customerKey, "Not found when attempting to fetch while retrying after concurrency modification.");
handleError(new SyncException(errorMessage, null), 1);
return CompletableFuture.completedFuture(null);
});
});
}
Aggregations