Search in sources :

Example 11 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class InventorySync method buildActionsAndUpdate.

/**
 * Given an existing {@link InventoryEntry} and a new {@link InventoryEntryDraft}, the method
 * calculates all the update actions required to synchronize the existing entry to be the same as
 * the new one. If there are update actions found, a request is made to CTP to update the existing
 * entry, otherwise it doesn't issue a request.
 *
 * <p>The {@code statistics} instance is updated accordingly to whether the CTP request was
 * carried out successfully or not. If an exception was thrown on executing the request to CTP,
 * the error handling method is called.
 *
 * @param entry existing inventory entry that could be updated.
 * @param draft draft containing data that could differ from data in {@code entry}. <strong>Sku
 *     isn't compared</strong>
 * @return a future which contains an empty result after execution of the update.
 */
private CompletionStage<Optional<InventoryEntry>> buildActionsAndUpdate(@Nonnull final InventoryEntry entry, @Nonnull final InventoryEntryDraft draft) {
    final List<UpdateAction<InventoryEntry>> updateActions = buildActions(entry, draft, syncOptions);
    final List<UpdateAction<InventoryEntry>> beforeUpdateCallBackApplied = syncOptions.applyBeforeUpdateCallback(updateActions, draft, entry);
    if (!beforeUpdateCallBackApplied.isEmpty()) {
        return inventoryService.updateInventoryEntry(entry, beforeUpdateCallBackApplied).handle(ImmutablePair::new).thenCompose(updateResponse -> {
            final InventoryEntry updatedInventoryEntry = updateResponse.getKey();
            final Throwable sphereException = updateResponse.getValue();
            if (sphereException != null) {
                final ResourceIdentifier<Channel> supplyChannel = draft.getSupplyChannel();
                final String errorMessage = format(CTP_INVENTORY_ENTRY_UPDATE_FAILED, draft.getSku(), supplyChannel != null ? supplyChannel.getId() : null);
                handleError(new SyncException(errorMessage, sphereException), entry, draft, updateActions);
                return CompletableFuture.completedFuture(Optional.empty());
            } else {
                statistics.incrementUpdated();
                return CompletableFuture.completedFuture(Optional.of(updatedInventoryEntry));
            }
        });
    }
    return completedFuture(null);
}
Also used : UpdateAction(io.sphere.sdk.commands.UpdateAction) Channel(io.sphere.sdk.channels.Channel) InventoryEntry(io.sphere.sdk.inventory.InventoryEntry) SyncException(com.commercetools.sync.commons.exceptions.SyncException)

Example 12 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class CustomerSync method updateCustomer.

@Nonnull
private CompletionStage<Void> updateCustomer(@Nonnull final Customer oldCustomer, @Nonnull final CustomerDraft newCustomerDraft, @Nonnull final List<UpdateAction<Customer>> updateActionsAfterCallback) {
    return customerService.updateCustomer(oldCustomer, updateActionsAfterCallback).handle(ImmutablePair::of).thenCompose(updateResponse -> {
        final Throwable exception = updateResponse.getValue();
        if (exception != null) {
            return executeSupplierIfConcurrentModificationException(exception, () -> fetchAndUpdate(oldCustomer, newCustomerDraft), () -> {
                final String errorMessage = format(CTP_CUSTOMER_UPDATE_FAILED, newCustomerDraft.getKey(), exception.getMessage());
                handleError(new SyncException(errorMessage, exception), 1);
                return CompletableFuture.completedFuture(null);
            });
        } else {
            statistics.incrementUpdated();
            return CompletableFuture.completedFuture(null);
        }
    });
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Example 13 with SyncException

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.
 * @param oldCustomObject existing custom object that could be updated.
 * @param newCustomObjectDraft draft containing data that could differ from data in {@code
 *     oldCustomObject}.
 */
private void handleError(@Nonnull final String errorMessage, @Nullable final Throwable exception, final int failedTimes, @Nullable final CustomObject<JsonNode> oldCustomObject, @Nullable final CustomObjectDraft<JsonNode> newCustomObjectDraft) {
    SyncException syncException = exception != null ? new SyncException(errorMessage, exception) : new SyncException(errorMessage);
    syncOptions.applyErrorCallback(syncException, oldCustomObject, newCustomObjectDraft, null);
    statistics.incrementFailed(failedTimes);
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException)

Example 14 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class TypeSync 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 types 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 types counter is incremented.
 * @param oldType existing type that could be updated.
 * @param newType draft containing data that could differ from data in {@code oldType}.
 * @param updateActions the update actions to update the {@link Type} with.
 */
private void handleError(@Nonnull final String errorMessage, @Nullable final Throwable exception, final int failedTimes, @Nullable final Type oldType, @Nullable final TypeDraft newType, @Nullable final List<UpdateAction<Type>> updateActions) {
    SyncException syncException = exception != null ? new SyncException(errorMessage, exception) : new SyncException(errorMessage);
    syncOptions.applyErrorCallback(syncException, oldType, newType, updateActions);
    statistics.incrementFailed(failedTimes);
}
Also used : SyncException(com.commercetools.sync.commons.exceptions.SyncException)

Example 15 with SyncException

use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.

the class TaxCategorySync method updateTaxCategory.

/**
 * Given an existing {@link TaxCategory} and a new {@link TaxCategoryDraft}, the method calculates
 * all the update actions required to synchronize the existing tax category to be the same as the
 * new one. If there are update actions found, a request is made to CTP to update the existing tax
 * category, otherwise it doesn't issue a request.
 *
 * <p>The {@code statistics} instance is updated accordingly to whether the CTP request was
 * carried out successfully or not. If an exception was thrown on executing the request to CTP,
 * the error handling method is called.
 *
 * @param oldTaxCategory existing tax category that could be updated.
 * @param newTaxCategory draft containing data that could differ from data in {@code
 *     oldTaxCategory}.
 * @return a {@link CompletionStage} which contains an empty result after execution of the update.
 */
@Nonnull
private CompletionStage<Optional<TaxCategory>> updateTaxCategory(@Nonnull final TaxCategory oldTaxCategory, @Nonnull final TaxCategoryDraft newTaxCategory, @Nonnull final List<UpdateAction<TaxCategory>> updateActions) {
    return taxCategoryService.updateTaxCategory(oldTaxCategory, updateActions).handle(ImmutablePair::new).thenCompose(updateResponse -> {
        final TaxCategory updatedTaxCategory = updateResponse.getKey();
        final Throwable sphereException = updateResponse.getValue();
        if (sphereException != null) {
            return executeSupplierIfConcurrentModificationException(sphereException, () -> fetchAndUpdate(oldTaxCategory, newTaxCategory), () -> {
                final String errorMessage = format(TAX_CATEGORY_UPDATE_FAILED, newTaxCategory.getKey(), sphereException.getMessage());
                handleError(new SyncException(errorMessage, sphereException), oldTaxCategory, newTaxCategory, updateActions, 1);
                return completedFuture(Optional.empty());
            });
        } else {
            statistics.incrementUpdated();
            return completedFuture(Optional.of(updatedTaxCategory));
        }
    });
}
Also used : TaxCategory(io.sphere.sdk.taxcategories.TaxCategory) SyncException(com.commercetools.sync.commons.exceptions.SyncException) Nonnull(javax.annotation.Nonnull)

Aggregations

SyncException (com.commercetools.sync.commons.exceptions.SyncException)74 Nonnull (javax.annotation.Nonnull)47 UpdateAction (io.sphere.sdk.commands.UpdateAction)45 Optional (java.util.Optional)39 List (java.util.List)37 CompletionStage (java.util.concurrent.CompletionStage)25 SphereClient (io.sphere.sdk.client.SphereClient)24 String.format (java.lang.String.format)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)20 Test (org.junit.jupiter.api.Test)20 TriConsumer (com.commercetools.sync.commons.utils.TriConsumer)18 Set (java.util.Set)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 Nullable (javax.annotation.Nullable)16 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Category (io.sphere.sdk.categories.Category)13 BaseSync (com.commercetools.sync.commons.BaseSync)12 QuadConsumer (com.commercetools.sync.commons.utils.QuadConsumer)12