Skip to content

Commit decccb2

Browse files
Add error callback arguments (#850)
1 parent 39edc9c commit decccb2

File tree

15 files changed

+386
-476
lines changed

15 files changed

+386
-476
lines changed

src/main/java/com/commercetools/sync/cartdiscounts/CartDiscountSync.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.commercetools.sync.cartdiscounts.helpers.CartDiscountReferenceResolver;
1414
import com.commercetools.sync.cartdiscounts.helpers.CartDiscountSyncStatistics;
1515
import com.commercetools.sync.commons.BaseSync;
16-
import com.commercetools.sync.commons.exceptions.SyncException;
1716
import com.commercetools.sync.services.CartDiscountService;
1817
import com.commercetools.sync.services.TypeService;
1918
import com.commercetools.sync.services.impl.CartDiscountServiceImpl;
@@ -28,14 +27,14 @@
2827
import java.util.concurrent.CompletableFuture;
2928
import java.util.concurrent.CompletionStage;
3029
import javax.annotation.Nonnull;
31-
import javax.annotation.Nullable;
3230
import org.apache.commons.lang3.tuple.ImmutablePair;
3331

3432
/**
3533
* This class syncs cart discount drafts with the corresponding cart discounts in the CTP project.
3634
*/
3735
public class CartDiscountSync
38-
extends BaseSync<CartDiscountDraft, CartDiscountSyncStatistics, CartDiscountSyncOptions> {
36+
extends BaseSync<
37+
CartDiscountDraft, CartDiscount, CartDiscountSyncStatistics, CartDiscountSyncOptions> {
3938

4039
private static final String CTP_CART_DISCOUNT_FETCH_FAILED =
4140
"Failed to fetch existing cart discounts with keys: '%s'.";
@@ -133,6 +132,9 @@ protected CompletionStage<CartDiscountSyncStatistics> processBatch(
133132
handleError(
134133
"Failed to build a cache of keys to ids.",
135134
cachingException,
135+
null,
136+
null,
137+
null,
136138
validDrafts.size());
137139
return CompletableFuture.completedFuture(null);
138140
}
@@ -150,7 +152,7 @@ protected CompletionStage<CartDiscountSyncStatistics> processBatch(
150152

151153
if (exception != null) {
152154
final String errorMessage = format(CTP_CART_DISCOUNT_FETCH_FAILED, keys);
153-
handleError(errorMessage, exception, keys.size());
155+
handleError(errorMessage, exception, null, null, null, keys.size());
154156
return CompletableFuture.completedFuture(null);
155157
} else {
156158
return syncBatch(fetchedCartDiscounts, validDrafts);
@@ -164,24 +166,6 @@ protected CompletionStage<CartDiscountSyncStatistics> processBatch(
164166
});
165167
}
166168

167-
/**
168-
* This method calls the optional error callback specified in the {@code syncOptions} and updates
169-
* the {@code statistics} instance by incrementing the total number of failed cart discounts to
170-
* sync.
171-
*
172-
* @param errorMessage The error message describing the reason(s) of failure.
173-
* @param exception The exception that called caused the failure, if any.
174-
* @param failedTimes The number of times that the failed cart discount statistic counter is
175-
* incremented.
176-
*/
177-
private void handleError(
178-
@Nonnull final String errorMessage,
179-
@Nullable final Throwable exception,
180-
final int failedTimes) {
181-
syncOptions.applyErrorCallback(new SyncException(errorMessage, exception));
182-
statistics.incrementFailed(failedTimes);
183-
}
184-
185169
/**
186170
* Given a set of cart discount drafts, attempts to sync the drafts with the existing cart
187171
* discounts in the CTP project. The cart discount and the draft are considered to match if they
@@ -213,7 +197,13 @@ private CompletionStage<Void> syncBatch(
213197
FAILED_TO_PROCESS,
214198
newCartDiscount.getKey(),
215199
completionException.getMessage());
216-
handleError(errorMessage, completionException, 1);
200+
handleError(
201+
errorMessage,
202+
completionException,
203+
null,
204+
newCartDiscount,
205+
null,
206+
1);
217207
return null;
218208
}))
219209
.map(CompletionStage::toCompletableFuture)
@@ -316,7 +306,13 @@ private CompletionStage<Void> updateCartDiscount(
316306
CTP_CART_DISCOUNT_UPDATE_FAILED,
317307
newCartDiscount.getKey(),
318308
sphereException.getMessage());
319-
handleError(errorMessage, sphereException, 1);
309+
handleError(
310+
errorMessage,
311+
sphereException,
312+
oldCartDiscount,
313+
newCartDiscount,
314+
updateActions,
315+
1);
320316
return CompletableFuture.completedFuture(null);
321317
});
322318
} else {
@@ -346,7 +342,7 @@ private CompletionStage<Void> fetchAndUpdate(
346342
CTP_CART_DISCOUNT_UPDATE_FAILED,
347343
key,
348344
"Failed to fetch from CTP while retrying after concurrency modification.");
349-
handleError(errorMessage, exception, 1);
345+
handleError(errorMessage, exception, oldCartDiscount, newCartDiscount, null, 1);
350346
return CompletableFuture.completedFuture(null);
351347
}
352348

@@ -362,7 +358,7 @@ private CompletionStage<Void> fetchAndUpdate(
362358
key,
363359
"Not found when attempting to fetch while retrying "
364360
+ "after concurrency modification.");
365-
handleError(errorMessage, null, 1);
361+
handleError(errorMessage, null, oldCartDiscount, newCartDiscount, null, 1);
366362
return CompletableFuture.completedFuture(null);
367363
});
368364
});

src/main/java/com/commercetools/sync/categories/CategorySync.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.commercetools.sync.categories.helpers.CategoryReferenceResolver;
1515
import com.commercetools.sync.categories.helpers.CategorySyncStatistics;
1616
import com.commercetools.sync.commons.BaseSync;
17-
import com.commercetools.sync.commons.exceptions.SyncException;
1817
import com.commercetools.sync.commons.models.WaitingToBeResolvedCategories;
1918
import com.commercetools.sync.services.CategoryService;
2019
import com.commercetools.sync.services.TypeService;
@@ -38,12 +37,11 @@
3837
import java.util.concurrent.ConcurrentHashMap;
3938
import java.util.stream.Collectors;
4039
import javax.annotation.Nonnull;
41-
import javax.annotation.Nullable;
4240
import org.apache.commons.lang3.StringUtils;
4341
import org.apache.commons.lang3.tuple.ImmutablePair;
4442

4543
public class CategorySync
46-
extends BaseSync<CategoryDraft, CategorySyncStatistics, CategorySyncOptions> {
44+
extends BaseSync<CategoryDraft, Category, CategorySyncStatistics, CategorySyncOptions> {
4745

4846
private static final String FAILED_TO_FETCH =
4947
"Failed to fetch existing categories with keys: '%s'. Reason: %s";
@@ -481,19 +479,4 @@ private CompletableFuture<Void> updateCategoriesSequentially(
481479
.forEach(CompletableFuture::join);
482480
return completedFuture(null);
483481
}
484-
485-
private void handleError(
486-
@Nonnull final String errorMessage,
487-
@Nullable final Throwable exception,
488-
@Nullable final Category oldCategory,
489-
@Nullable final CategoryDraft newCategory,
490-
@Nullable final List<UpdateAction<Category>> updateActions,
491-
final int failedTimes) {
492-
SyncException syncException =
493-
exception != null
494-
? new SyncException(errorMessage, exception)
495-
: new SyncException(errorMessage);
496-
syncOptions.applyErrorCallback(syncException, oldCategory, newCategory, updateActions);
497-
statistics.incrementFailed(failedTimes);
498-
}
499482
}

src/main/java/com/commercetools/sync/commons/BaseSync.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.commercetools.sync.commons;
22

3+
import com.commercetools.sync.commons.exceptions.SyncException;
34
import com.commercetools.sync.commons.helpers.BaseSyncStatistics;
45
import io.sphere.sdk.client.ConcurrentModificationException;
6+
import io.sphere.sdk.commands.UpdateAction;
7+
import io.sphere.sdk.models.Versioned;
58
import java.util.List;
69
import java.util.concurrent.CompletionStage;
710
import java.util.function.Supplier;
811
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
913

10-
public abstract class BaseSync<T, U extends BaseSyncStatistics, V extends BaseSyncOptions> {
14+
public abstract class BaseSync<
15+
T, S extends Versioned, U extends BaseSyncStatistics, V extends BaseSyncOptions> {
1116
protected final U statistics;
1217
protected final V syncOptions;
1318

@@ -122,4 +127,32 @@ protected static <S> S executeSupplierIfConcurrentModificationException(
122127
}
123128
return onOtherExceptionSupplier.get();
124129
}
130+
131+
/**
132+
* This method calls the optional error callback specified in the {@code syncOptions} and updates
133+
* the {@code statistics} instance by incrementing the total number of failed resources S to sync.
134+
*
135+
* @param errorMessage The error message describing the reason(s) of failure.
136+
* @param exception The exception that called caused the failure, if any.
137+
* @param oldResource the commercetools resource which could be updated.
138+
* @param newResourceDraft the commercetools resource draft where we get the new data.
139+
* @param updateActions the update actions to update the resource with.
140+
* @param failedTimes The number of times that the failed cart discount statistic counter is
141+
* incremented.
142+
*/
143+
@SuppressWarnings("unchecked")
144+
protected void handleError(
145+
@Nonnull final String errorMessage,
146+
@Nullable final Throwable exception,
147+
final S oldResource,
148+
final T newResourceDraft,
149+
final List<UpdateAction<S>> updateActions,
150+
final int failedTimes) {
151+
final SyncException syncException =
152+
exception != null
153+
? new SyncException(errorMessage, exception)
154+
: new SyncException(errorMessage);
155+
syncOptions.applyErrorCallback(syncException, oldResource, newResourceDraft, updateActions);
156+
statistics.incrementFailed(failedTimes);
157+
}
125158
}

src/main/java/com/commercetools/sync/customers/CustomerSync.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static java.util.stream.Collectors.toSet;
1010

1111
import com.commercetools.sync.commons.BaseSync;
12-
import com.commercetools.sync.commons.exceptions.SyncException;
1312
import com.commercetools.sync.customers.helpers.CustomerBatchValidator;
1413
import com.commercetools.sync.customers.helpers.CustomerReferenceResolver;
1514
import com.commercetools.sync.customers.helpers.CustomerSyncStatistics;
@@ -33,7 +32,7 @@
3332

3433
/** This class syncs customer drafts with the corresponding customers in the CTP project. */
3534
public class CustomerSync
36-
extends BaseSync<CustomerDraft, CustomerSyncStatistics, CustomerSyncOptions> {
35+
extends BaseSync<CustomerDraft, Customer, CustomerSyncStatistics, CustomerSyncOptions> {
3736

3837
private static final String CTP_CUSTOMER_FETCH_FAILED =
3938
"Failed to fetch existing customers with keys: '%s'.";
@@ -130,7 +129,11 @@ protected CompletionStage<CustomerSyncStatistics> processBatch(
130129
final Throwable cachingException = cachingResponse.getValue();
131130
if (cachingException != null) {
132131
handleError(
133-
new SyncException("Failed to build a cache of keys to ids.", cachingException),
132+
"Failed to build a cache of keys to ids.",
133+
cachingException,
134+
null,
135+
null,
136+
null,
134137
validCustomerDrafts.size());
135138
return CompletableFuture.completedFuture(null);
136139
}
@@ -150,7 +153,7 @@ protected CompletionStage<CustomerSyncStatistics> processBatch(
150153
final String errorMessage =
151154
format(CTP_CUSTOMER_FETCH_FAILED, validCustomerKeys);
152155
handleError(
153-
new SyncException(errorMessage, exception), validCustomerKeys.size());
156+
errorMessage, exception, null, null, null, validCustomerKeys.size());
154157
return CompletableFuture.completedFuture(null);
155158
} else {
156159
return syncBatch(fetchedCustomers, validCustomerDrafts);
@@ -188,7 +191,8 @@ private CompletionStage<Void> syncBatch(
188191
FAILED_TO_PROCESS,
189192
customerDraft.getKey(),
190193
completionException.getMessage());
191-
handleError(new SyncException(errorMessage, completionException), 1);
194+
handleError(
195+
errorMessage, completionException, null, customerDraft, null, 1);
192196
return null;
193197
}))
194198
.map(CompletionStage::toCompletableFuture)
@@ -245,7 +249,13 @@ private CompletionStage<Void> updateCustomer(
245249
CTP_CUSTOMER_UPDATE_FAILED,
246250
newCustomerDraft.getKey(),
247251
exception.getMessage());
248-
handleError(new SyncException(errorMessage, exception), 1);
252+
handleError(
253+
errorMessage,
254+
exception,
255+
oldCustomer,
256+
newCustomerDraft,
257+
updateActionsAfterCallback,
258+
1);
249259
return CompletableFuture.completedFuture(null);
250260
});
251261
} else {
@@ -274,7 +284,7 @@ private CompletionStage<Void> fetchAndUpdate(
274284
CTP_CUSTOMER_UPDATE_FAILED,
275285
customerKey,
276286
"Failed to fetch from CTP while retrying after concurrency modification.");
277-
handleError(new SyncException(errorMessage, exception), 1);
287+
handleError(errorMessage, exception, oldCustomer, newCustomerDraft, null, 1);
278288
return CompletableFuture.completedFuture(null);
279289
}
280290

@@ -287,7 +297,7 @@ private CompletionStage<Void> fetchAndUpdate(
287297
CTP_CUSTOMER_UPDATE_FAILED,
288298
customerKey,
289299
"Not found when attempting to fetch while retrying after concurrency modification.");
290-
handleError(new SyncException(errorMessage, null), 1);
300+
handleError(errorMessage, null, oldCustomer, newCustomerDraft, null, 1);
291301
return CompletableFuture.completedFuture(null);
292302
});
293303
});
@@ -312,9 +322,4 @@ private CompletionStage<Void> applyCallbackAndCreate(@Nonnull final CustomerDraf
312322
}))
313323
.orElseGet(() -> CompletableFuture.completedFuture(null));
314324
}
315-
316-
private void handleError(@Nonnull final SyncException syncException, final int failedTimes) {
317-
syncOptions.applyErrorCallback(syncException);
318-
statistics.incrementFailed(failedTimes);
319-
}
320325
}

0 commit comments

Comments
 (0)