Merge pull request #1050 from CartoDB/prevent-layert-stats-undefined-access

Prevent "Cannot read property 'geom_type' of undefined" on layer stats
This commit is contained in:
Raul Ochoa 2018-10-23 18:37:00 +02:00 committed by GitHub
commit 33bcac189f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -71,7 +71,7 @@ function _geometryType(ctx) {
const geometryColumn = AggregationMapConfig.getAggregationGeometryColumn();
const sqlQuery = _getSQL(ctx, sql => queryUtils.getQueryGeometryType(sql, geometryColumn));
return queryUtils.queryPromise(ctx.dbConnection, sqlQuery)
.then(res => ({ geometryType: res.rows[0].geom_type }));
.then(res => ({ geometryType: (res.rows[0] || {}).geom_type }));
}
return Promise.resolve();
}

View File

@ -2,14 +2,32 @@ require('../../support/test_helper');
var assert = require('../../support/assert');
var TestClient = require('../../support/test-client');
const serverOptions = require('../../../lib/cartodb/server_options');
const suites = [{
desc: 'mvt (mapnik)',
usePostGIS: false
}];
if (process.env.POSTGIS_VERSION >= '20400') {
suites.push({
desc: 'mvt (postgis)',
usePostGIS: true
});
}
suites.forEach(({desc, usePostGIS}) => {
describe(`[${desc}] Create mapnik layergroup`, function() {
const originalUsePostGIS = serverOptions.renderer.mvt.usePostGIS;
describe('Create mapnik layergroup', function() {
before(function() {
serverOptions.renderer.mvt.usePostGIS = usePostGIS;
this.layerStatsConfig = global.environment.enabledFeatures.layerStats;
global.environment.enabledFeatures.layerStats = true;
});
after(function() {
serverOptions.renderer.mvt.usePostGIS = originalUsePostGIS;
global.environment.enabledFeatures.layerStats = this.layerStatsConfig;
});
@ -519,6 +537,30 @@ describe('Create mapnik layergroup', function() {
});
});
it(`should not fail "TypeError: ... 'geom_type' of undefined" for empty results`, function(done) {
var testClient = new TestClient({
version: '1.8.0',
layers: [
{
type: 'mapnik',
options: {
sql: 'select * from test_table where false',
metadata: {
geometryType: true
}
}
}
]
});
testClient.getLayergroup(function(err, layergroup) {
assert.ifError(err);
assert.equal(layergroup.metadata.layers[0].id, mapnikBasicLayerId(0));
assert.equal(layergroup.metadata.layers[0].meta.stats.geometryType, undefined);
testClient.drain(done);
});
});
it('should provide a sample as optional metadata', function(done) {
var testClient = new TestClient({
version: '1.4.0',
@ -589,3 +631,4 @@ describe('Create mapnik layergroup', function() {
});
});
});