diff --git a/lib/cartodb/controllers/map/anonymous.js b/lib/cartodb/controllers/map/anonymous.js index 665ba799..b90c6d0b 100644 --- a/lib/cartodb/controllers/map/anonymous.js +++ b/lib/cartodb/controllers/map/anonymous.js @@ -19,6 +19,7 @@ const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layer const layerStats = require('./middlewares/layer-stats'); const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupMetadata = require('./middlewares/layergroup-metadata'); +const mapError = require('./middlewares/map-error'); const sendResponse = require('../../middleware/send-response'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const LayergroupMetadata = require('../../utils/layergroup-metadata'); @@ -108,7 +109,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () { layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupMetadata(this.layergroupMetadata, includeQuery), sendResponse(), - augmentError({ label, addContext }) + mapError({ label, addContext }) ]; }; @@ -215,39 +216,3 @@ function createLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTabl }); }; } - -function augmentError (options) { - const { addContext = false, label = 'MAPS CONTROLLER' } = options; - - return function augmentErrorMiddleware (err, req, res, next) { - req.profiler.done('error'); - const { mapConfig } = res.locals; - - if (addContext) { - err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err; - } - - err.label = label; - - next(err); - }; -} - -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; -} diff --git a/lib/cartodb/controllers/map/middlewares/map-error.js b/lib/cartodb/controllers/map/middlewares/map-error.js new file mode 100644 index 00000000..6d87a93d --- /dev/null +++ b/lib/cartodb/controllers/map/middlewares/map-error.js @@ -0,0 +1,35 @@ +module.exports = function mapError (options) { + const { addContext = false, label = 'MAPS CONTROLLER' } = options; + + return function mapErrorMiddleware (err, req, res, next) { + req.profiler.done('error'); + const { mapConfig } = res.locals; + + if (addContext) { + err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err; + } + + err.label = label; + + next(err); + }; +}; + +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; +} diff --git a/lib/cartodb/controllers/map/named.js b/lib/cartodb/controllers/map/named.js index 5707053a..b29cb06f 100644 --- a/lib/cartodb/controllers/map/named.js +++ b/lib/cartodb/controllers/map/named.js @@ -1,4 +1,3 @@ -const _ = require('underscore'); const ResourceLocator = require('../../models/resource-locator'); const cleanUpQueryParams = require('../../middleware/clean-up-query-params'); const credentials = require('../../middleware/credentials'); @@ -16,6 +15,7 @@ const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layer const layerStats = require('./middlewares/layer-stats'); const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupMetadata = require('./middlewares/layergroup-metadata'); +const mapError = require('./middlewares/map-error'); const sendResponse = require('../../middleware/send-response'); const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); @@ -119,7 +119,7 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function () layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupMetadata(this.layergroupMetadata, includeQuery), sendResponse(), - augmentError({ label, addContext }) + mapError({ label, addContext }) ]; }; @@ -222,39 +222,3 @@ function instantiateLayergroup (mapBackend, userLimitsApi, pgConnection, affecte }); }; } - -function augmentError (options) { - const { addContext = false, label = 'MAPS CONTROLLER' } = options; - - return function augmentErrorMiddleware (err, req, res, next) { - req.profiler.done('error'); - const { mapConfig } = res.locals; - - if (addContext) { - err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err; - } - - err.label = label; - - next(err); - }; -} - -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; -}