From 468d2be66860da098a45c39025374841031ac70c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 14 Jan 2015 11:47:01 -0500 Subject: [PATCH] added error checks and missing returns to widgets closes #2611 --- src/widgets.js | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/widgets.js b/src/widgets.js index 0390b2d1f0..7a46fc7e9b 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -12,12 +12,13 @@ var async = require('async'), Widgets.render = function(uid, area, callback) { if (!area.locations || !area.template) { - callback({ - error: 'Missing location and template data' - }); + return callback(new Error('[[error:invalid-data]]')); } Widgets.getAreas(['global', area.template], area.locations, function(err, data) { + if (err) { + return callback(err); + } var widgetsByLocation = {}; @@ -39,6 +40,10 @@ var async = require('async'), area: area, data: widget.data }, function(err, html) { + if (err) { + return next(err); + } + if (widget.data.container && widget.data.container.match('{body}')) { html = templates.parse(widget.data.container, { title: widget.data.title, @@ -46,7 +51,7 @@ var async = require('async'), }); } - next(err, {html: html}); + next(null, {html: html}); }); }, function(err, widgets) { done(err, {location: location, widgets: widgets.filter(Boolean)}); @@ -70,7 +75,12 @@ var async = require('async'), returnData[template] = returnData[template] || {}; locations.forEach(function(location) { if (data && data[index] && data[index][location]) { - returnData[template][location] = JSON.parse(data[index][location]); + try { + returnData[template][location] = JSON.parse(data[index][location]); + } catch(err) { + winston.error('can not parse widget data. template: ' + template + ' location: ' + location); + returnData[template][location] = []; + } } else { returnData[template][location] = []; } @@ -83,23 +93,28 @@ var async = require('async'), Widgets.getArea = function(template, location, callback) { db.getObjectField('widgets:' + template, location, function(err, widgets) { + if (err) { + return callback(err); + } if (!widgets) { - return callback(err, []); + return callback(null, []); } - callback(err, JSON.parse(widgets)); + try { + widgets = JSON.parse(widgets); + } catch(err) { + return callback(err); + } + + callback(null, widgets); }); }; Widgets.setArea = function(area, callback) { if (!area.location || !area.template) { - callback({ - error: 'Missing location and template data' - }); + return callback(new Error('Missing location and template data')); } - db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), function(err) { - callback(err); - }); + db.setObjectField('widgets:' + area.template, area.location, JSON.stringify(area.widgets), callback); }; Widgets.reset = function(callback) { @@ -108,10 +123,17 @@ var async = require('async'), ]; plugins.fireHook('filter:widgets.getAreas', defaultAreas, function(err, areas) { + if (err) { + return callback(err); + } var drafts = []; async.each(areas, function(area, next) { Widgets.getArea(area.template, area.location, function(err, areaData) { + if (err) { + return callback(err); + } + drafts = drafts.concat(areaData); area.widgets = []; Widgets.setArea(area, next);