Browse Source

refactor elasticsearch error detection, improve test coverage

pull/533/head
Peter Johnson 9 years ago
parent
commit
b15cfb3795
  1. 26
      middleware/sendJSON.js
  2. 2
      test/ciao_test_data.js
  3. 200
      test/unit/middleware/sendJSON.js
  4. 1
      test/unit/run.js

26
middleware/sendJSON.js

@ -1,4 +1,5 @@
var check = require('check-types'); var check = require('check-types'),
es = require('elasticsearch');
function sendJSONResponse(req, res, next) { function sendJSONResponse(req, res, next) {
@ -21,17 +22,18 @@ function sendJSONResponse(req, res, next) {
geocoding.errors.forEach( function( err ){ geocoding.errors.forEach( function( err ){
// custom status codes for instances of the Error() object. // custom status codes for instances of the Error() object.
if( err instanceof Error ){ if( err instanceof Error ){
// we can extract the error type from the constructor name /*
switch( err.constructor.name ){ elasticsearch errors
// elasticsearch errors see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js
// see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js
case 'RequestTimeout': statusCode = 408; break; // 408 Request Timeout 408 Request Timeout
case 'NoConnections': statusCode = 502; break; // 502 Bad Gateway 500 Internal Server Error
case 'ConnectionFault': statusCode = 502; break; // 502 Bad Gateway 502 Bad Gateway
case 'Serialization': statusCode = 500; break; // 500 Internal Server Error */
case 'Generic': statusCode = 500; break; // 500 Internal Server Error if( err instanceof es.errors.RequestTimeout ){ statusCode = 408; }
default: statusCode = 500; // 500 Internal Server Error else if( err instanceof es.errors.NoConnections ){ statusCode = 502; }
} else if( err instanceof es.errors.ConnectionFault ){ statusCode = 502; }
else { statusCode = 500; }
} }
}); });
} }

2
test/ciao_test_data.js

@ -54,5 +54,5 @@ actions.push( function( done ){
// perform all actions in series // perform all actions in series
async.series( actions, function( err, resp ){ async.series( actions, function( err, resp ){
console.log('test data inported'); console.log('test data imported');
}); });

200
test/unit/middleware/sendJSON.js

@ -0,0 +1,200 @@
var es = require('elasticsearch'),
middleware = require('../../../middleware/sendJSON');
module.exports.tests = {};
module.exports.tests.invalid = function(test, common) {
test('invalid $res', function(t) {
var res;
middleware(null, res, function () {
t.pass('next() called.');
t.end();
});
});
test('invalid $res.body', function(t) {
var res = { body: 1 };
middleware(null, res, function () {
t.pass('next() called.');
t.end();
});
});
test('invalid $res.body.geocoding', function(t) {
var res = { body: { geocoding: 1 } };
middleware(null, res, function () {
t.pass('next() called.');
t.end();
});
});
};
module.exports.tests.default_status = function(test, common) {
test('no errors', function(t) {
var res = { body: { geocoding: {} } };
res.status = function( code ){
return { json: function( body ){
t.equal( code, 200, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
test('empty errors array', function(t) {
var res = { body: { geocoding: {}, errors: [] } };
res.status = function( code ){
return { json: function( body ){
t.equal( code, 200, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.default_error_status = function(test, common) {
test('default error code', function(t) {
var res = { body: { geocoding: {
errors: [ 'an error' ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 400, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.generic_server_error = function(test, common) {
test('generic server error', function(t) {
var res = { body: { geocoding: {
errors: [ new Error('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 500, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.generic_elasticsearch_error = function(test, common) {
test('generic elasticsearch error', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.Generic('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 500, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.request_timeout = function(test, common) {
test('request timeout', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.RequestTimeout('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 408, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.no_connections = function(test, common) {
test('no connections', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.NoConnections('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 502, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.connection_fault = function(test, common) {
test('connection fault', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.ConnectionFault('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 502, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.tests.serialization = function(test, common) {
test('serialization', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.Serialization('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 500, 'default status' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('[middleware] sendJSON: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

1
test/unit/run.js

@ -27,6 +27,7 @@ var tests = [
require('./middleware/localNamingConventions'), require('./middleware/localNamingConventions'),
require('./middleware/dedupe'), require('./middleware/dedupe'),
require('./middleware/parseBBox'), require('./middleware/parseBBox'),
require('./middleware/sendJSON'),
require('./middleware/normalizeParentIds'), require('./middleware/normalizeParentIds'),
require('./query/autocomplete'), require('./query/autocomplete'),
require('./query/autocomplete_defaults'), require('./query/autocomplete_defaults'),

Loading…
Cancel
Save