Fix jshint max-complexity issue by using extract method

This commit is contained in:
Daniel García Aubert 2017-10-30 19:29:19 +01:00
parent b6c405bf68
commit fb3afaa6ab

View File

@ -184,38 +184,49 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
}, },
function finish(err, layergroup) { function finish(err, layergroup) {
if (err) { if (err) {
if (Number.isFinite(err.layerIndex)) { err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err;
var error = new Error(err.message);
error.http_status = err.http_status;
if (!err.http_status && err.message.indexOf('column "the_geom_webmercator" does not exist') >= 0) {
error.http_status = 400;
}
error.type = 'layer';
error.subtype = err.message.indexOf('Postgis Plugin') >= 0 ? 'query' : undefined;
error.layer = {
id: mapConfig.getLayerId(err.layerIndex),
index: err.layerIndex,
type: mapConfig.layerType(err.layerIndex)
};
err = error;
}
err.label = 'ANONYMOUS LAYERGROUP'; err.label = 'ANONYMOUS LAYERGROUP';
next(err);
return next(err);
}
var analysesResults = context.analysesResults || [];
self.addDataviewsAndWidgetsUrls(res.locals.user, layergroup, mapConfig.obj());
self.addAnalysesMetadata(res.locals.user, layergroup, analysesResults, true);
addContextMetadata(layergroup, mapConfig.obj(), context);
res.set('X-Layergroup-Id', layergroup.layergroupid);
res.status(200);
if (req.query && req.query.callback) {
res.jsonp(layergroup);
} else { } else {
var analysesResults = context.analysesResults || []; res.json(layergroup);
self.addDataviewsAndWidgetsUrls(res.locals.user, layergroup, mapConfig.obj());
self.addAnalysesMetadata(res.locals.user, layergroup, analysesResults, true);
addContextMetadata(layergroup, mapConfig.obj(), context);
res.set('X-Layergroup-Id', layergroup.layergroupid);
self.send(req, res, layergroup, 200);
} }
} }
); );
}; };
function populateError(err, mapConfig) {
var error = new Error(err.message);
error.http_status = err.http_status;
if (!err.http_status && err.message.indexOf('column "the_geom_webmercator" does not exist') >= 0) {
error.http_status = 400;
}
error.type = 'layer';
error.subtype = err.message.indexOf('Postgis Plugin') >= 0 ? 'query' : undefined;
error.layer = {
id: mapConfig.getLayerId(err.layerIndex),
index: err.layerIndex,
type: mapConfig.layerType(err.layerIndex)
};
return error;
}
function addContextMetadata(layergroup, mapConfig, context) { function addContextMetadata(layergroup, mapConfig, context) {
if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) {
layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) { layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {