use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class StateBatchValidator method getTransitionKeys.
@Nonnull
private static Set<String> getTransitionKeys(@Nonnull final StateDraft stateDraft) throws SyncException {
final Set<Reference<State>> transitions = stateDraft.getTransitions();
if (transitions == null || transitions.isEmpty()) {
return emptySet();
}
final Set<String> referencedStateKeys = new HashSet<>();
final List<Reference<State>> invalidStates = new ArrayList<>();
for (Reference<State> transition : transitions) {
if (transition != null) {
try {
referencedStateKeys.add(getStateKey(transition));
} catch (InvalidReferenceException invalidReferenceException) {
invalidStates.add(transition);
}
}
}
if (!invalidStates.isEmpty()) {
final String errorMessage = format(STATE_HAS_INVALID_REFERENCES, stateDraft.getKey());
throw new SyncException(errorMessage, new InvalidReferenceException(BLANK_ID_VALUE_ON_REFERENCE));
}
return referencedStateKeys;
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomerServiceImpl method createCustomer.
@Nonnull
@Override
public CompletionStage<Optional<Customer>> createCustomer(@Nonnull final CustomerDraft customerDraft) {
// Uses a different implementation than in the base service because CustomerCreateCommand uses a
// different library as CTP responds with a CustomerSignInResult which is not extending resource
// but a
// different model, containing the customer resource.
final String draftKey = customerDraft.getKey();
final CustomerCreateCommand createCommand = CustomerCreateCommand.of(customerDraft);
if (isBlank(draftKey)) {
syncOptions.applyErrorCallback(new SyncException(format(CREATE_FAILED, draftKey, "Draft key is blank!")), null, customerDraft, null);
return CompletableFuture.completedFuture(Optional.empty());
} else {
return syncOptions.getCtpClient().execute(createCommand).handle(((resource, exception) -> {
if (exception == null && resource.getCustomer() != null) {
keyToIdCache.put(draftKey, resource.getCustomer().getId());
return Optional.of(resource.getCustomer());
} else if (exception != null) {
syncOptions.applyErrorCallback(new SyncException(format(CREATE_FAILED, draftKey, exception.getMessage()), exception), null, customerDraft, null);
return Optional.empty();
} else {
return Optional.empty();
}
}));
}
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-sync-java by commercetools.
the class CustomUpdateActionUtilsTest method buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack.
@Test
void buildCustomUpdateActions_WithNullOldCustomFieldsAndBlankNewTypeId_ShouldCallErrorCallBack() {
final Asset oldAsset = mock(Asset.class);
final String oldAssetId = "oldAssetId";
when(oldAsset.getId()).thenReturn(oldAssetId);
when(oldAsset.getCustom()).thenReturn(null);
final AssetDraft newAssetDraft = AssetDraftBuilder.of(emptyList(), ofEnglish("assetName")).custom(ofTypeKeyAndJson("key", new HashMap<>())).build();
// Mock custom options error callback
final ArrayList<Object> callBackResponses = new ArrayList<>();
final QuadConsumer<SyncException, Optional<ProductDraft>, Optional<ProductProjection>, List<UpdateAction<Product>>> errorCallback = (exception, newResource, oldResource, updateActions) -> {
callBackResponses.add(exception.getMessage());
callBackResponses.add(exception.getCause());
};
// Mock sync options
final ProductSyncOptions productSyncOptions = ProductSyncOptionsBuilder.of(CTP_CLIENT).errorCallback(errorCallback).build();
final List<UpdateAction<Product>> updateActions = buildCustomUpdateActions(maiNewResource, oldAsset, newAssetDraft, new AssetCustomActionBuilder(), 10, Asset::getId, asset -> Asset.resourceTypeId(), Asset::getKey, productSyncOptions);
assertThat(updateActions).isNotNull();
assertThat(updateActions).hasSize(0);
assertThat(callBackResponses.get(0)).isEqualTo(format("Failed to build custom fields update actions on the " + "asset with id '%s'. Reason: New resource's custom type id is blank (empty/null).", oldAssetId));
assertThat(callBackResponses.get(1)).isNull();
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-project-sync by commercetools.
the class SyncUtilsTest method logErrorCallbackWithStringResourceIdentifierAndNullUpdateActions_ShouldLogErrorWithCorrectMessage.
@Test
void logErrorCallbackWithStringResourceIdentifierAndNullUpdateActions_ShouldLogErrorWithCorrectMessage() {
final TestLogger testLogger = TestLoggerFactory.getTestLogger(SyncUtilsTest.class);
SyncException exception = new SyncException("test sync exception");
logErrorCallback(testLogger, "test resource", exception, "test identifier", null);
assertThat(testLogger.getAllLoggingEvents()).hasSize(1);
final LoggingEvent loggingEvent = testLogger.getAllLoggingEvents().get(0);
assertThat(loggingEvent.getMessage()).isEqualTo("Error when trying to sync test resource. Existing key: test identifier. Update actions: []");
assertThat(loggingEvent.getThrowable().isPresent()).isTrue();
assertThat(loggingEvent.getThrowable().get()).isInstanceOf(SyncException.class);
}
use of com.commercetools.sync.commons.exceptions.SyncException in project commercetools-project-sync by commercetools.
the class SyncUtilsTest method logWarningCallbackStringResourceIdentifier_ShouldLogWarningWithCorrectMessage.
@Test
void logWarningCallbackStringResourceIdentifier_ShouldLogWarningWithCorrectMessage() {
final TestLogger testLogger = TestLoggerFactory.getTestLogger(SyncUtilsTest.class);
SyncException exception = new SyncException("test sync exception");
logWarningCallback(testLogger, "test resource", exception, "test identifier");
assertThat(testLogger.getAllLoggingEvents()).hasSize(1);
final LoggingEvent loggingEvent = testLogger.getAllLoggingEvents().get(0);
assertThat(loggingEvent.getMessage()).isEqualTo("Warning when trying to sync test resource. Existing key: test identifier");
assertThat(loggingEvent.getThrowable().isPresent()).isTrue();
assertThat(loggingEvent.getThrowable().get()).isInstanceOf(SyncException.class);
}
Aggregations