Browse Source

feat(coarse_reverse): add try/catch block around synthesizeDoc code

pull/1003/head
missinglink 7 years ago
parent
commit
c72822b805
  1. 15
      controller/coarse_reverse.js
  2. 54
      test/unit/controller/coarse_reverse.js

15
controller/coarse_reverse.js

@ -62,6 +62,7 @@ function synthesizeDoc(results) {
const most_granular_layer = getMostGranularLayerOfResult(_.keys(results)); const most_granular_layer = getMostGranularLayerOfResult(_.keys(results));
const id = results[most_granular_layer][0].id; const id = results[most_granular_layer][0].id;
try {
const doc = new Document('whosonfirst', most_granular_layer, id.toString()); const doc = new Document('whosonfirst', most_granular_layer, id.toString());
doc.setName('default', results[most_granular_layer][0].name); doc.setName('default', results[most_granular_layer][0].name);
@ -96,6 +97,15 @@ function synthesizeDoc(results) {
esDoc.data._type = esDoc._type; esDoc.data._type = esDoc._type;
return esDoc.data; return esDoc.data;
} catch( e ) {
// an error occurred when generating a new Document
logger.info(`[controller:coarse_reverse][error]`);
logger.error(e);
logger.error(results);
return null;
}
} }
function setup(service, should_execute) { function setup(service, should_execute) {
@ -141,7 +151,10 @@ function setup(service, should_execute) {
// if there's a result at the requested layer(s), synthesize a doc from results // if there's a result at the requested layer(s), synthesize a doc from results
if (hasResultsAtRequestedLayers(applicable_results, effective_layers)) { if (hasResultsAtRequestedLayers(applicable_results, effective_layers)) {
res.data.push(synthesizeDoc(applicable_results)); const doc = synthesizeDoc(applicable_results);
if (doc){
res.data.push(doc);
}
} }
debugLog.stopTimer(req, initialTime); debugLog.stopTimer(req, initialTime);
return next(); return next();

54
test/unit/controller/coarse_reverse.js

@ -860,6 +860,60 @@ module.exports.tests.failure_conditions = (test, common) => {
}); });
test('service returns 0 length name', (t) => {
t.plan(5);
const service = (req, callback) => {
t.deepEquals(req, { clean: { layers: ['neighbourhood'] } } );
const results = {
neighbourhood: [
{ id: 20, name: '' }
]
};
callback(undefined, results);
};
const logger = require('pelias-mock-logger')();
const controller = proxyquire('../../../controller/coarse_reverse', {
'pelias-logger': logger
})(service, _.constant(true));
const req = {
clean: {
layers: ['neighbourhood']
}
};
const res = { };
// verify that next was called
const next = () => {
t.pass('next() was called');
};
controller(req, res, next);
const expected = {
meta: {},
data: []
};
t.deepEquals(res, expected);
t.deepEquals(logger.getMessages('info'), [
'[controller:coarse_reverse][queryType:pip][result_count:1]',
'[controller:coarse_reverse][error]'
]);
t.deepEquals(logger.getMessages('error'), [
'{ [PeliasModelError: invalid document type, expecting: truthy, ' +
'got: ]\n name: \'PeliasModelError\',\n message: \'invalid document type, expecting: truthy, got: \' }',
'{ neighbourhood: [ { id: 20, name: \'\' } ] }'
]);
t.end();
});
}; };
module.exports.all = (tape, common) => { module.exports.all = (tape, common) => {

Loading…
Cancel
Save