From 9377b73aa30201c3d8c983b056b004474191b153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 28 Mar 2018 14:12:21 +0200 Subject: [PATCH] Extract map error middleware --- lib/cartodb/controllers/map/anonymous.js | 39 +----------------- .../controllers/map/middlewares/map-error.js | 35 ++++++++++++++++ lib/cartodb/controllers/map/named.js | 40 +------------------ 3 files changed, 39 insertions(+), 75 deletions(-) create mode 100644 lib/cartodb/controllers/map/middlewares/map-error.js 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; -}