From 92e62069d4bda3705373d9234341f78b3a49d7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Thu, 28 Feb 2019 12:13:15 +0100 Subject: [PATCH] Improve error handling --- lib/cartodb/backends/cluster.js | 13 +++++++-- test/acceptance/cluster.js | 52 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/cartodb/backends/cluster.js b/lib/cartodb/backends/cluster.js index baa48523..2b93f2ee 100644 --- a/lib/cartodb/backends/cluster.js +++ b/lib/cartodb/backends/cluster.js @@ -22,13 +22,22 @@ module.exports = class ClusterBackend { const { user, token, layer: layerIndex } = params; const mapConfig = new AggregationMapConfig(user, _mapConfig.obj(), pg); - if (!mapConfig.isAggregationLayer(layerIndex)) { + const layer = mapConfig.getLayer(layerIndex); + + if (layer.options.aggregation === false || !mapConfig.isAggregationLayer(layerIndex)) { const error = new Error(`Map ${token} has no aggregation defined for layer ${layerIndex}`); + error.http_status = 400; + error.type = 'layer'; + error.subtype = 'aggregation'; + error.layer = { + index: layerIndex, + type: layer.type + }; + debug(error); return callback(error); } - const layer = mapConfig.getLayer(layerIndex); const query = layer.options.sql_raw; const resolution = layer.options.aggregation.resolution || 1; diff --git a/test/acceptance/cluster.js b/test/acceptance/cluster.js index 82ec5ff8..a31af1b7 100644 --- a/test/acceptance/cluster.js +++ b/test/acceptance/cluster.js @@ -61,11 +61,59 @@ describe('cluster', function () { errors:[ 'Map d725a568ab961af8197d311eececb83a has no aggregation defined for layer 0' ], errors_with_context:[ { - type: 'unknown', - message: 'Map d725a568ab961af8197d311eececb83a has no aggregation defined for layer 0' + layer: { + index: '0', + type: 'cartodb' + }, + message: 'Map d725a568ab961af8197d311eececb83a has no aggregation defined for layer 0', + subtype: 'aggregation', + type: 'layer' } ] }); + + testClient.drain(done); + }); + }); + + it('with aggregation disabled should return error while fetching disaggregated features', function (done) { + const mapConfig = createVectorMapConfig([{ + type: 'cartodb', + options: { + sql: POINTS_SQL_1, + aggregation: false + } + }]); + const testClient = new TestClient(mapConfig); + const zoom = 0; + const cartodb_id = 1; + const layerId = 0; + const params = { + response: { + status: 400 + } + }; + + testClient.getClusterFeatures(zoom, cartodb_id, layerId, params, (err, body) => { + if (err) { + return done(err); + } + + assert.deepStrictEqual(body, { + errors:[ 'Map 3a09728f8c08444820336ea9983ce92b has no aggregation defined for layer 0' ], + errors_with_context:[ + { + layer: { + index: '0', + type: 'cartodb' + }, + message: 'Map 3a09728f8c08444820336ea9983ce92b has no aggregation defined for layer 0', + subtype: 'aggregation', + type: 'layer' + } + ] + }); + testClient.drain(done); }); });