diff --git a/controller/place.js b/controller/place.js index 29acfe8b..a4996a11 100644 --- a/controller/place.js +++ b/controller/place.js @@ -22,7 +22,7 @@ function setup( backend ){ }; }); - logger.debug( '[ES req]', JSON.stringify(query) ); + logger.debug( '[ES req]', query ); service.mget( backend, query, function( err, docs ) { console.log('err:' + err); @@ -35,7 +35,7 @@ function setup( backend ){ else { res.data = docs; } - logger.debug('[ES response]', JSON.stringify(docs)); + logger.debug('[ES response]', docs); next(); }); diff --git a/controller/search.js b/controller/search.js index 7949c8ad..e2a8acff 100644 --- a/controller/search.js +++ b/controller/search.js @@ -31,7 +31,7 @@ function setup( backend, query ){ cmd.type = req.clean.layers; } - logger.debug( '[ES req]', JSON.stringify(cmd) ); + logger.debug( '[ES req]', cmd ); // query backend service.search( backend, cmd, function( err, docs, meta ){ @@ -49,7 +49,7 @@ function setup( backend, query ){ res.data = docs; res.meta = meta; } - logger.debug('[ES response]', JSON.stringify(docs)); + logger.debug('[ES response]', docs); next(); }); diff --git a/middleware/sendJSON.js b/middleware/sendJSON.js index 94353c6c..be2e56b0 100644 --- a/middleware/sendJSON.js +++ b/middleware/sendJSON.js @@ -1,4 +1,12 @@ -var check = require('check-types'); +var check = require('check-types'), + es = require('elasticsearch'), + exceptions = require('elasticsearch-exceptions/lib/exceptions/SupportedExceptions'); + +// create a list of regular expressions to match against. +// note: list created when the server starts up; for performance reasons. +var exceptionRegexList = exceptions.map( function( exceptionName ){ + return new RegExp( '^' + exceptionName ); +}); function sendJSONResponse(req, res, next) { @@ -8,10 +16,11 @@ function sendJSONResponse(req, res, next) { } // default status - var statusCode = 200; + var statusCode = 200; // 200 OK // vary status code whenever an error was reported var geocoding = res.body.geocoding; + if( check.array( geocoding.errors ) && geocoding.errors.length ){ // default status for errors is 400 Bad Request @@ -19,18 +28,33 @@ function sendJSONResponse(req, res, next) { // iterate over all reported errors geocoding.errors.forEach( function( err ){ + // custom status codes for instances of the Error() object. if( err instanceof Error ){ - // we can extract the error type from the constructor name - switch( err.constructor.name ){ - // elasticsearch errors - // see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js - case 'RequestTimeout': statusCode = 408; break; // 408 Request Timeout - case 'NoConnections': statusCode = 502; break; // 502 Bad Gateway - case 'ConnectionFault': statusCode = 502; break; // 502 Bad Gateway - case 'Serialization': statusCode = 500; break; // 500 Internal Server Error - case 'Generic': statusCode = 500; break; // 500 Internal Server Error - default: statusCode = 500; // 500 Internal Server Error + /* + elasticsearch errors + see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js + + 408 Request Timeout + 500 Internal Server Error + 502 Bad Gateway + */ + if( err instanceof es.errors.RequestTimeout ){ statusCode = Math.max( statusCode, 408 ); } + else if( err instanceof es.errors.NoConnections ){ statusCode = Math.max( statusCode, 502 ); } + else if( err instanceof es.errors.ConnectionFault ){ statusCode = Math.max( statusCode, 502 ); } + else { statusCode = Math.max( statusCode, 500 ); } + + /* + some elasticsearch errors are only returned as strings (not instances of Error). + in this case we (unfortunately) need to match the exception at position 0 inside the string. + */ + } else if( check.string( err ) ){ + for( var i=0; i