From 3c1d7582298b6faf483169cd4af64d33fac13c97 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 14 Apr 2016 15:35:11 -0400 Subject: [PATCH] Fix timeout error handling Timeout errors come back from the Elasticsearch module as Error objects, so instead of pushing the entire object onto the errors array, we should just push the message. I'm not sure if there are other cases where errors are just strings, errors that aren't objects with a message property are handled as before. --- controller/search.js | 8 +++++++- test/unit/controller/search.js | 17 ++++++++++++++++- test/unit/mock/backend.js | 8 ++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/controller/search.js b/controller/search.js index a564e7b4..7949c8ad 100644 --- a/controller/search.js +++ b/controller/search.js @@ -1,3 +1,5 @@ +var _ = require('lodash'); + var service = { search: require('../service/search') }; var logger = require('pelias-logger').get('api:controller:search'); @@ -36,7 +38,11 @@ function setup( backend, query ){ // error handler if( err ){ - req.errors.push( err ); + if (_.isObject(err) && err.message) { + req.errors.push( err.message ); + } else { + req.errors.push( err ); + } } // set response data else { diff --git a/test/unit/controller/search.js b/test/unit/controller/search.js index cfb715fa..09127167 100644 --- a/test/unit/controller/search.js +++ b/test/unit/controller/search.js @@ -123,6 +123,21 @@ module.exports.tests.functional_failure = function(test, common) { }); }; +module.exports.tests.timeout = function(test, common) { + test('timeout', function(t) { + var backend = mockBackend( 'client/search/timeout/1', function( cmd ){ + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var req = { clean: { a: 'b' }, errors: [], warnings: [] }; + var next = function(){ + t.equal(req.errors[0],'Request Timeout after 5000ms'); + t.end(); + }; + controller(req, undefined, next ); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { @@ -132,4 +147,4 @@ module.exports.all = function (tape, common) { for( var testCase in module.exports.tests ){ module.exports.tests[testCase](test, common); } -}; \ No newline at end of file +}; diff --git a/test/unit/mock/backend.js b/test/unit/mock/backend.js index 84147783..7d347a30 100644 --- a/test/unit/mock/backend.js +++ b/test/unit/mock/backend.js @@ -33,6 +33,14 @@ responses['client/search/fail/1'] = function( cmd, cb ){ return cb( 'a backend error occurred' ); }; +responses['client/search/timeout/1'] = function( cmd, cb) { + // timeout errors are objects + return cb({ + status: 408, + message: 'Request Timeout after 5000ms' + }); +}; + responses['client/mget/ok/1'] = function( cmd, cb ){ return cb( undefined, mgetEnvelope([{ _id: 'myid1',