Extract map error middleware

This commit is contained in:
Daniel García Aubert 2018-03-28 14:12:21 +02:00
parent e5aff3f366
commit 9377b73aa3
3 changed files with 39 additions and 75 deletions

View File

@ -19,6 +19,7 @@ const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layer
const layerStats = require('./middlewares/layer-stats'); const layerStats = require('./middlewares/layer-stats');
const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupIdHeader = require('./middlewares/layergroup-id-header');
const layergroupMetadata = require('./middlewares/layergroup-metadata'); const layergroupMetadata = require('./middlewares/layergroup-metadata');
const mapError = require('./middlewares/map-error');
const sendResponse = require('../../middleware/send-response'); const sendResponse = require('../../middleware/send-response');
const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider');
const LayergroupMetadata = require('../../utils/layergroup-metadata'); const LayergroupMetadata = require('../../utils/layergroup-metadata');
@ -108,7 +109,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () {
layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupIdHeader(this.templateMaps ,useTemplateHash),
layergroupMetadata(this.layergroupMetadata, includeQuery), layergroupMetadata(this.layergroupMetadata, includeQuery),
sendResponse(), 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;
}

View File

@ -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;
}

View File

@ -1,4 +1,3 @@
const _ = require('underscore');
const ResourceLocator = require('../../models/resource-locator'); const ResourceLocator = require('../../models/resource-locator');
const cleanUpQueryParams = require('../../middleware/clean-up-query-params'); const cleanUpQueryParams = require('../../middleware/clean-up-query-params');
const credentials = require('../../middleware/credentials'); const credentials = require('../../middleware/credentials');
@ -16,6 +15,7 @@ const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layer
const layerStats = require('./middlewares/layer-stats'); const layerStats = require('./middlewares/layer-stats');
const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupIdHeader = require('./middlewares/layergroup-id-header');
const layergroupMetadata = require('./middlewares/layergroup-metadata'); const layergroupMetadata = require('./middlewares/layergroup-metadata');
const mapError = require('./middlewares/map-error');
const sendResponse = require('../../middleware/send-response'); const sendResponse = require('../../middleware/send-response');
const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider'); const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider');
const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider');
@ -119,7 +119,7 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function ()
layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupIdHeader(this.templateMaps ,useTemplateHash),
layergroupMetadata(this.layergroupMetadata, includeQuery), layergroupMetadata(this.layergroupMetadata, includeQuery),
sendResponse(), 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;
}