manage JSON parse and stringify sinc errors

This commit is contained in:
Simon Martín
2018-08-01 15:48:24 +02:00
parent 7b7bee2901
commit 0e28348e16
+16 -3
View File
@@ -297,7 +297,6 @@ TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) {
// @param callback function(err)
//
TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) {
template = templateDefaults(template);
var invalidError = this._checkInvalidTemplate(template);
@@ -320,7 +319,14 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback)
return callback(new Error(`Template '${tpl_id}' of user '${owner}' does not exist`));
}
this._redisCmd('HSET', [userTemplatesKey, template.name, JSON.stringify(template)], (err, didSetNewField) => {
let templateString;
try {
templateString = JSON.stringify(template)
} catch (error) {
return callback(error);
}
this._redisCmd('HSET', [userTemplatesKey, template.name, templateString], (err, didSetNewField) => {
if (err) {
return callback(err);
}
@@ -329,7 +335,14 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback)
debug('New template created on update operation');
}
if (this.fingerPrint(JSON.parse(beforeUpdateTemplate)) !== this.fingerPrint(template)) {
let beforeUpdateTemplateObject;
try {
beforeUpdateTemplateObject = JSON.parse(beforeUpdateTemplate)
} catch (error) {
return callback(error);
}
if (this.fingerPrint(beforeUpdateTemplateObject) !== this.fingerPrint(template)) {
this.emit('update', owner, template.name, template);
}