Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions controller/coarse_reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,50 @@ function synthesizeDoc(results) {
const most_granular_layer = getMostGranularLayerOfResult(_.keys(results));
const id = results[most_granular_layer][0].id;

const doc = new Document('whosonfirst', most_granular_layer, id.toString());
doc.setName('default', results[most_granular_layer][0].name);
try {
const doc = new Document('whosonfirst', most_granular_layer, id.toString());
doc.setName('default', results[most_granular_layer][0].name);

// assign the administrative hierarchy
_.keys(results).forEach((layer) => {
doc.addParent(layer, results[layer][0].name, results[layer][0].id.toString(), results[layer][0].abbr);
});
// assign the administrative hierarchy
_.keys(results).forEach((layer) => {
doc.addParent(layer, results[layer][0].name, results[layer][0].id.toString(), results[layer][0].abbr);
});

// set centroid if available
if (_.has(results[most_granular_layer][0], 'centroid')) {
doc.setCentroid( results[most_granular_layer][0].centroid );
}
// set centroid if available
if (_.has(results[most_granular_layer][0], 'centroid')) {
doc.setCentroid( results[most_granular_layer][0].centroid );
}

// set bounding box if available
if (_.has(results[most_granular_layer][0], 'bounding_box')) {
const parsed_bounding_box = results[most_granular_layer][0].bounding_box.split(',').map(parseFloat);
doc.setBoundingBox({
upperLeft: {
lat: parsed_bounding_box[3],
lon: parsed_bounding_box[0]
},
lowerRight: {
lat: parsed_bounding_box[1],
lon: parsed_bounding_box[2]
}
});
// set bounding box if available
if (_.has(results[most_granular_layer][0], 'bounding_box')) {
const parsed_bounding_box = results[most_granular_layer][0].bounding_box.split(',').map(parseFloat);
doc.setBoundingBox({
upperLeft: {
lat: parsed_bounding_box[3],
lon: parsed_bounding_box[0]
},
lowerRight: {
lat: parsed_bounding_box[1],
lon: parsed_bounding_box[2]
}
});

}
}

const esDoc = doc.toESDocument();
esDoc.data._id = esDoc._id;
esDoc.data._type = esDoc._type;
return esDoc.data;

const esDoc = doc.toESDocument();
esDoc.data._id = esDoc._id;
esDoc.data._type = esDoc._type;
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) {
Expand Down Expand Up @@ -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 (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);
return next();
Expand Down
54 changes: 54 additions & 0 deletions test/unit/controller/coarse_reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down