use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class TaxCategorySync method fetchAndUpdate.
@Nonnull
private CompletionStage<Optional<TaxCategory>> fetchAndUpdate(@Nonnull final TaxCategory oldTaxCategory, @Nonnull final TaxCategoryDraft newTaxCategory) {
final String key = oldTaxCategory.getKey();
return taxCategoryService.fetchTaxCategory(key).handle(ImmutablePair::new).thenCompose(fetchResponse -> {
final Optional<TaxCategory> fetchedTaxCategoryOptional = fetchResponse.getKey();
final Throwable exception = fetchResponse.getValue();
if (exception != null) {
final String errorMessage = format(TAX_CATEGORY_UPDATE_FAILED, key, "Failed to fetch from CTP while retrying after concurrency modification.");
handleError(new SyncException(errorMessage, exception), oldTaxCategory, newTaxCategory, null, 1);
return completedFuture(null);
}
return fetchedTaxCategoryOptional.map(fetchedTaxCategory -> buildActionsAndUpdate(fetchedTaxCategory, newTaxCategory)).orElseGet(() -> {
final String errorMessage = format(TAX_CATEGORY_UPDATE_FAILED, key, "Not found when attempting to fetch while retrying " + "after concurrency modification.");
handleError(new SyncException(errorMessage), oldTaxCategory, newTaxCategory, null, 1);
return completedFuture(null);
});
});
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CartDiscountSync method handleError.
/**
* 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 cart discounts 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 cart discount statistic counter is
* incremented.
*/
private void handleError(@Nonnull final String errorMessage, @Nullable final Throwable exception, final int failedTimes) {
syncOptions.applyErrorCallback(new SyncException(errorMessage, exception));
statistics.incrementFailed(failedTimes);
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CategorySync method handleError.
private void handleError(@Nonnull final String errorMessage, @Nullable final Throwable exception, @Nullable final Category oldCategory, @Nullable final CategoryDraft newCategory, @Nullable final List<UpdateAction<Category>> updateActions, final int failedTimes) {
SyncException syncException = exception != null ? new SyncException(errorMessage, exception) : new SyncException(errorMessage);
syncOptions.applyErrorCallback(syncException, oldCategory, newCategory, updateActions);
statistics.incrementFailed(failedTimes);
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class ProductSyncWithReferencedProductsInAnyOrderIT method sync_withFailToFetchCustomObject_shouldSyncCorrectly.
@SuppressWarnings("unchecked")
@Test
void sync_withFailToFetchCustomObject_shouldSyncCorrectly() {
// preparation
final String productReferenceAttributeName = "product-reference";
final String parentProductKey = "parent-product-key";
final AttributeDraft productReferenceAttribute = AttributeDraft.of(productReferenceAttributeName, Reference.of(Product.referenceTypeId(), parentProductKey));
final ProductDraft childDraft1 = ProductDraftBuilder.of(productType, ofEnglish("foo"), ofEnglish("foo-slug"), ProductVariantDraftBuilder.of().key("foo").sku("foo").attributes(productReferenceAttribute).build()).key(product.getKey()).build();
final ProductDraft parentDraft = ProductDraftBuilder.of(productType, ofEnglish(parentProductKey), ofEnglish(parentProductKey), ProductVariantDraftBuilder.of().sku(parentProductKey).key(parentProductKey).build()).key(parentProductKey).build();
final SphereClient ctpClient = spy(CTP_TARGET_CLIENT);
final BadGatewayException gatewayException = new BadGatewayException("failed to respond.");
when(ctpClient.execute(any(CustomObjectQuery.class))).thenReturn(CompletableFutureUtils.failed(gatewayException)).thenCallRealMethod();
syncOptions = ProductSyncOptionsBuilder.of(ctpClient).errorCallback((syncException, draft, product, updateActions) -> collectErrors(syncException.getMessage(), syncException)).beforeUpdateCallback(this::collectActions).build();
// test
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = productSync.sync(singletonList(childDraft1)).thenCompose(ignoredResult -> productSync.sync(singletonList(parentDraft))).toCompletableFuture().join();
// assertion
assertThat(syncStatistics).hasValues(2, 1, 0, 1, 0);
assertThat(errorCallBackMessages).containsExactly("Failed to fetch ProductDrafts waiting to be resolved with keys '[foo]'.");
assertThat(errorCallBackExceptions).singleElement(as(THROWABLE)).isExactlyInstanceOf(SyncException.class).hasCauseExactlyInstanceOf(CompletionException.class).hasRootCauseExactlyInstanceOf(BadGatewayException.class);
assertThat(warningCallBackMessages).isEmpty();
assertThat(actions).isEmpty();
final UnresolvedReferencesService<WaitingToBeResolvedProducts> unresolvedReferencesService = new UnresolvedReferencesServiceImpl<>(syncOptions);
final Set<WaitingToBeResolvedProducts> waitingDrafts = unresolvedReferencesService.fetch(asSet(childDraft1.getKey()), CUSTOM_OBJECT_PRODUCT_CONTAINER_KEY, WaitingToBeResolvedProducts.class).toCompletableFuture().join();
assertThat(waitingDrafts).containsExactly(new WaitingToBeResolvedProducts(childDraft1, singleton(parentProductKey)));
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class ProductSyncIT method sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel.
@Test
void sync_withMissingPriceChannel_shouldCreateProductDistributionPriceChannel() {
PriceDraftDsl priceDraftWithMissingChannelRef = PriceDraftBuilder.of(MoneyImpl.of("20", "EUR")).channel(ResourceIdentifier.ofKey("missingKey")).build();
ProductVariantDraftDsl masterVariantDraft = ProductVariantDraftBuilder.of(ProductVariantDraftDsl.of().withKey("v2").withSku("1065833").withPrices(Collections.singletonList(priceDraftWithMissingChannelRef))).build();
final ProductDraft productDraft = createProductDraftBuilder(PRODUCT_KEY_2_RESOURCE_PATH, ResourceIdentifier.ofKey(productType.getKey())).masterVariant(masterVariantDraft).taxCategory(null).state(null).build();
final TriConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>> warningCallBack = (exception, newResource, oldResource) -> warningCallBackMessages.add(exception.getMessage());
ProductSyncOptions syncOptions = ProductSyncOptionsBuilder.of(CTP_TARGET_CLIENT).errorCallback((exception, oldResource, newResource, actions) -> collectErrors(exception.getMessage(), exception.getCause())).warningCallback(warningCallBack).ensurePriceChannels(true).build();
final ProductSync productSync = new ProductSync(syncOptions);
final ProductSyncStatistics syncStatistics = executeBlocking(productSync.sync(singletonList(productDraft)));
assertThat(syncStatistics).hasValues(1, 1, 0, 0, 0);
assertThat(errorCallBackExceptions).isEmpty();
assertThat(errorCallBackMessages).isEmpty();
assertThat(warningCallBackMessages).isEmpty();
Product productFromTargetProject = executeBlocking(CTP_TARGET_CLIENT.execute(ProductByKeyGet.of(productDraft.getKey())));
List<Price> prices = productFromTargetProject.getMasterData().getStaged().getMasterVariant().getPrices();
assertThat(prices.size()).isEqualTo(1);
Channel channel = executeBlocking(CTP_TARGET_CLIENT.execute(ChannelByIdGet.of(Objects.requireNonNull(Objects.requireNonNull(prices.get(0).getChannel()).getId()))));
assertThat(channel.getRoles()).containsOnly(ChannelRole.PRODUCT_DISTRIBUTION);
}
Aggregations