From 6bbaeaa2860337782e6be604fc2d00c02c62da86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 31 Oct 2017 20:49:26 +0100 Subject: [PATCH] Create a custom error middleware to augment error info --- lib/cartodb/controllers/map.js | 85 ++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 054a901e..82a7732b 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -56,28 +56,42 @@ MapController.prototype.register = function(app) { cors(), userMiddleware, this.prepareContext, - this.createGet.bind(this) + this.createGet.bind(this), + mapErrorMiddleware({ + label: 'ANONYMOUS LAYERGROUP', + augmentError: true + }) ); app.post( app.base_url_mapconfig, cors(), userMiddleware, this.prepareContext, - this.createPost.bind(this) + this.createPost.bind(this), + mapErrorMiddleware({ + label: 'ANONYMOUS LAYERGROUP', + augmentError: true + }) ); app.get( app.base_url_templated + '/:template_id/jsonp', cors(), userMiddleware, this.prepareContext, - this.jsonp.bind(this) + this.jsonp.bind(this), + mapErrorMiddleware({ + label: 'NAMED MAP LAYERGROUP' + }) ); app.post( app.base_url_templated + '/:template_id', cors(), userMiddleware, this.prepareContext, - this.instantiate.bind(this) + this.instantiate.bind(this), + mapErrorMiddleware({ + label: 'NAMED MAP LAYERGROUP' + }) ); app.options(app.base_url_mapconfig, cors('Content-Type')); }; @@ -170,7 +184,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) { function createLayergroup(err, requestMapConfig) { assert.ifError(err); var datasource = context.datasource || Datasource.EmptyDatasource(); - mapConfig = new MapConfig(requestMapConfig, datasource); + res.locals.mapconfig = mapConfig = new MapConfig(requestMapConfig, datasource); self.mapBackend.createLayergroup( mapConfig, res.locals, @@ -181,7 +195,6 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) { function afterLayergroupCreate(err, layergroup) { assert.ifError(err); - res.locals.mapconfig = mapConfig; res.locals.analysesResults = context.analysesResults; res.locals.layergroup = layergroup; res.locals.context = context; @@ -194,8 +207,6 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) { }, function finish(err) { if (err) { - err = Number.isFinite(err.layerIndex) ? populateError(err, mapConfig) : err; - err.label = 'ANONYMOUS LAYERGROUP'; return next(err); } @@ -212,25 +223,6 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) { ); }; -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; -} - MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn, next) { var self = this; @@ -260,7 +252,7 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn }, function createLayergroup(err, mapConfig_, rendererParams) { assert.ifError(err); - mapConfig = mapConfig_; + res.locals.mapconfig = mapConfig = mapConfig_; self.mapBackend.createLayergroup( mapConfig, rendererParams, new CreateLayergroupMapConfigProvider(mapConfig, cdbuser, self.userLimitsApi, rendererParams), @@ -270,7 +262,6 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn function afterLayergroupCreate(err, layergroup) { assert.ifError(err); - res.locals.mapconfig = mapConfig; res.locals.analysesResults = mapConfigProvider.analysesResults; res.locals.layergroup = layergroup; res.locals.template = mapConfigProvider.template; @@ -285,7 +276,6 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn }, function finishTemplateInstantiation(err) { if (err) { - err.label = 'NAMED MAP LAYERGROUP'; return next(err); } @@ -640,3 +630,38 @@ MapController.prototype.addWidgetsUrl = function(username, layergroup, mapConfig }.bind(this)); } }; + +function mapErrorMiddleware (options) { + const { augmentError = false, label = 'MAPS CONTROLLER' } = options; + + return function mapError (err, req, res, next) { + const { mapconfig } = res.locals; + + if (augmentError) { + 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; +}