diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index d1f67813..f1bbe55e 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -184,38 +184,49 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) { }, function finish(err, layergroup) { if (err) { - if (Number.isFinite(err.layerIndex)) { - var error = new Error(err.message); - error.http_status = err.http_status; + err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err; - 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'; - 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 { - 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); - self.send(req, res, layergroup, 200); + res.json(layergroup); } } ); }; +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) { if (layergroup.metadata && Array.isArray(layergroup.metadata.layers) && Array.isArray(mapConfig.layers)) { layergroup.metadata.layers = layergroup.metadata.layers.map(function(layer, layerIndex) {