Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle emojis errors in delta jobs #185

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,33 @@ function moveFile(sourceFile, targetFile) {
return success;
}

/**
* Moves files generated by the delta export to another location
* @param {string[]} deltaExportZipList List of delta export zip names
* @param {dw.io.File} sourceDir Directory to move from
* @param {dw.io.File} destDir Target directory
*/
function moveDeltaExportFiles(deltaExportZipList, sourceDir, destDir) {
if (empty(deltaExportZipList)) {
return;
}
deltaExportZipList.forEach(function (filename) {
let currentZipFile = new dw.io.File(sourceDir, filename); // 000001.zip, 000002.zip, etc.
let targetZipFile = new dw.io.File(destDir, currentZipFile.getName());
currentZipFile.renameTo(targetZipFile);

let currentMetaFile = new dw.io.File(sourceDir, filename.replace('.zip', '.meta')); // each .zip has a corresponding .meta file
let targetMetaFile = new dw.io.File(destDir, currentMetaFile.getName());
currentMetaFile.renameTo(targetMetaFile)
});
}

module.exports = {
getFirstChildFolder: getFirstChildFolder,
getDeltaExportZipList: getDeltaExportZipList,
getChildFolders: getChildFolders,
getAllXMLFilesInFolder: getAllXMLFilesInFolder,
removeFolderRecursively: removeFolderRecursively,
moveFile: moveFile,
moveDeltaExportFiles: moveDeltaExportFiles,
}
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,16 @@ function updateCPObjectFromXML(xmlFile, changedProducts, resourceType) {
var XMLStreamReader = require('dw/io/XMLStreamReader');
var XMLStreamConstants = require('dw/io/XMLStreamConstants');
var FileReader = require('dw/io/FileReader');
var catalogID;
var resultObj = {
nrProductsRead: 0,
success: false
success: false,
errorMessage: ''
};

try {
if (xmlFile.exists()) {
var fileReader = new FileReader(xmlFile);
var xmlStreamReader = new XMLStreamReader(fileReader);
var success = false;

switch (resourceType) {
case 'catalog':
Expand Down Expand Up @@ -575,9 +574,15 @@ function updateCPObjectFromXML(xmlFile, changedProducts, resourceType) {
}

xmlStreamReader.close();
};
}
} catch (error) {
algoliaLogger.error('Error while reading from file: ' + xmlFile.getFullPath());
algoliaLogger.error(error);
resultObj.success = false;
resultObj.errorMessage =
'Error while reading from file: ' + xmlFile.getFullPath() + '\n' +
error.message + '\n' +
'If your product attributes have special characters such as emojis, you must enable "Filter Invalid XML Characters during Export" in Administration > Global Preferences > Import & Export';
return resultObj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var fileHelper, jobHelper, modelHelper, reindexHelper, sendHelper;
// logging-related variables and constants
var jobReport;

var l0_deltaExportDir, l1_processingDir, l1_completedDir;
var l0_deltaExportDir, l1_processingDir, l1_completedDir, l1_failedDir;
var changedProducts = [], changedProductsIterator;
var deltaExportZips, siteLocales, attributesToSend;
var masterAttributes = [], variantAttributes = [];
Expand Down Expand Up @@ -233,6 +233,9 @@ exports.beforeStep = function(parameters, stepExecution) {
l1_completedDir = new File(l0_deltaExportDir, '_completed');
l1_completedDir.mkdir(); // creating "_completed" folder -- does no harm if it already exists

l1_failedDir = new File(l0_deltaExportDir, '_failed');
l1_failedDir.mkdir();

// process each export zip one by one
deltaExportZips.forEach(function(filename) {
logger.info('Processing ' + filename + '...');
Expand Down Expand Up @@ -268,13 +271,9 @@ exports.beforeStep = function(parameters, stepExecution) {
if (result.success) {
jobReport.processedItems += result.nrProductsRead;
} else {
// abort if error reading from any of the delta export zips
let errorMessage = 'Error reading from file: ' + catalogFile.getFullPath();
jobHelper.logError(errorMessage);

// Mark the job in error if an error occurred while reading from any of the delta export zips
jobReport.error = true;
jobReport.errorMessage = errorMessage;
jobReport.writeToCustomObject();
jobReport.errorMessage = result.errorMessage;
}
});
}
Expand All @@ -287,8 +286,13 @@ exports.beforeStep = function(parameters, stepExecution) {
// cleanup - removing "_processing" dir
fileHelper.removeFolderRecursively(l1_processingDir);

changedProductsIterator = new CPObjectIterator(changedProducts);
logger.info(jobReport.processedItems + ' updated products found. Starting indexing...');
if (!jobReport.error) {
changedProductsIterator = new CPObjectIterator(changedProducts);
logger.info(jobReport.processedItems + ' updated products found. Starting indexing...');
} else {
logger.info('Moving the Delta export files to the "' + l1_failedDir.getName() + '" directory...')
fileHelper.moveDeltaExportFiles(deltaExportZips, l0_deltaExportDir, l1_failedDir);
}
}

/**
Expand Down Expand Up @@ -467,15 +471,7 @@ exports.afterStep = function(success, parameters, stepExecution) {
// cleanup: after the products have successfully been sent, move the delta zips from which the productIDs have successfully been extracted and the corresponding products sent to "_completed"
if (!empty(deltaExportZips)) {
logger.info('Moving the Delta export files to the "_completed" directory...')
deltaExportZips.forEach(function (filename) {
let currentZipFile = new File(l0_deltaExportDir, filename); // 000001.zip, 000002.zip, etc.
let targetZipFile = new File(l1_completedDir, currentZipFile.getName());
fileHelper.moveFile(currentZipFile, targetZipFile);

let currentMetaFile = new File(l0_deltaExportDir, filename.replace('.zip', '.meta')); // each .zip has a corresponding .meta file as well, we'll need to delete these later
let targetMetaFile = new File(l1_completedDir, currentMetaFile.getName());
fileHelper.moveFile(currentMetaFile, targetMetaFile);
});
fileHelper.moveDeltaExportFiles(deltaExportZips, l0_deltaExportDir, l1_completedDir);
}
} else {
jobReport.error = true;
Expand Down