I've created a personalized synchronization process that queues up all my sync records in sequence. When my service retrieves multiple sync records, it processes them and updates the last sync date for successful records, or logs errors for failed records (without updating the last sync date) and stops the sync process.
To achieve this, I have utilized $q.all from AngularJS. Here's an excerpt from the sync loop:
var processes = [];
for (var i in data) {
// Checking for valid data fields
if (data[i] === null || data[i].TableName == null || data[i].Query == null || data[i].Params == null) {
throw new TypeError("ERROR! Unexpected data retrieved from the download sync process.");
}
// Processing parameters
var params = data[i].Params;
var paramsMassaged = params.replaceAll("[", "").replaceAll("]", "").replaceAll(", ", ",").replaceAll("'", "");
var paramsArray = paramsMassaged.split(",");
mlog.Log("Query: " + data[i].Query);
mlog.Log("Params: " + paramsArray);
// Handling different table cases
if (data[i].TableName === "table1" || data[i].TableName === "table2") {
var process = $DBContext.ExecuteSyncItem(data[i].Query, paramsArray);
process.then(
function () {
$DBConfigurations_DBContext.UpdateLastSyncDate(data[i].CreatedDate, function (response) {
mlog.Log(response);
});
},
function (response) {
mlog.LogSync("Error syncing record: " + response, "ERROR", data[i].Id);
},
null
);
processes.push(process);
} else {
mlog.LogSync("WARNING! Excluded table from sync process. Outdated application version. Table: " + data[i].TableName);
}
}
// Running all processes concurrently using $q.all
$q.all(processes)
.then(function (result) {
mlog.LogSync("---Finished syncing all records");
}, function (response) {
mlog.LogSync("Sync Failure - " + response, "ERROR");
});
For instance, consider the ExecuteSyncItem function below:
ExecuteSyncItem: function (script, params) {
window.logger.logIt("In the ExecuteSyncItem function...");
var primaryKey = params[params.length - 1];
var deferred = $q.defer();
$DBService.ExecuteQuery(script, params,
function (insertId, rowsAffected, rows) {
window.logger.logIt("rowsAffected: " + rowsAffected.rowsAffected);
if (rowsAffected.rowsAffected <= 1) {
deferred.resolve();
} else {
deferred.resolve(errorMessage);
}
},
function (tx, error) {
deferred.reject("Failed to sync record with primary key: " + primaryKey + "; Error: " + error.message);
}
);
return deferred.promise;
}
The issue arises when multiple sync records fail, causing the same error message to be displayed for each failure. How can I ensure that specific information is shown for each failing record instead of repeating the same message?