mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 16:05:49 +01:00
added error checks and missing returns to widgets closes #2611
This commit is contained in:
@@ -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]) {
|
||||
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 (!widgets) {
|
||||
return callback(err, []);
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(err, JSON.parse(widgets));
|
||||
if (!widgets) {
|
||||
return callback(null, []);
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user