Improve error handling

This commit is contained in:
Daniel García Aubert 2019-02-28 12:13:15 +01:00
parent 32938eeab7
commit 92e62069d4
2 changed files with 61 additions and 4 deletions

View File

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

View File

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