Browse Source

Merge pull request #333 from pelias/http_status_codes

http friendly status codes
pull/335/head
Peter Johnson a.k.a. insertcoffee 9 years ago
parent
commit
a03a18ef67
  1. 1
      app.js
  2. 2
      controller/place.js
  3. 2
      controller/search.js
  4. 15
      middleware/408.js
  5. 35
      middleware/sendJSON.js
  6. 2
      test/ciao/autocomplete/focus_point_invalid_lat.coffee
  7. 2
      test/ciao/autocomplete/focus_point_invalid_lon.coffee
  8. 2
      test/ciao/autocomplete/focus_point_missing_lat.coffee
  9. 2
      test/ciao/autocomplete/focus_point_missing_lon.coffee
  10. 2
      test/ciao/autocomplete/no_params.coffee
  11. 2
      test/ciao/autocomplete/text_invalid.coffee
  12. 2
      test/ciao/place/missing_id.coffee
  13. 2
      test/ciao/reverse/boundary_circle_invalid_radius.coffee
  14. 2
      test/ciao/reverse/boundary_country_invalid_alpha2.coffee
  15. 2
      test/ciao/reverse/boundary_country_invalid_alpha3.coffee
  16. 2
      test/ciao/reverse/boundary_country_invalid_iso3166.coffee
  17. 2
      test/ciao/reverse/duplicate_parameter_name.coffee
  18. 2
      test/ciao/reverse/layers_invalid.coffee
  19. 2
      test/ciao/reverse/layers_mix_invalid_valid.coffee
  20. 2
      test/ciao/reverse/non_scalar_parameter.coffee
  21. 2
      test/ciao/reverse/point_invalid_lat.coffee
  22. 2
      test/ciao/reverse/point_invalid_lon.coffee
  23. 2
      test/ciao/reverse/point_missing_lat.coffee
  24. 2
      test/ciao/reverse/point_missing_lon.coffee
  25. 2
      test/ciao/reverse/sources_invalid.coffee
  26. 2
      test/ciao/reverse/sources_layers_invalid_combo.coffee
  27. 2
      test/ciao/search/boundary_circle_invalid_lat_lon_types.coffee
  28. 2
      test/ciao/search/boundary_circle_invalid_radius.coffee
  29. 2
      test/ciao/search/boundary_circle_missing_lat.coffee
  30. 2
      test/ciao/search/boundary_circle_missing_lon.coffee
  31. 2
      test/ciao/search/boundary_country_invalid_alpha2.coffee
  32. 2
      test/ciao/search/boundary_country_invalid_alpha3.coffee
  33. 2
      test/ciao/search/boundary_country_invalid_iso3166.coffee
  34. 2
      test/ciao/search/boundary_rect_partially_specified.coffee
  35. 2
      test/ciao/search/focus_point_invalid_lat.coffee
  36. 2
      test/ciao/search/focus_point_invalid_lon.coffee
  37. 2
      test/ciao/search/focus_point_missing_lat.coffee
  38. 2
      test/ciao/search/focus_point_missing_lon.coffee
  39. 2
      test/ciao/search/layers_invalid.coffee
  40. 2
      test/ciao/search/layers_mix_invalid_valid.coffee
  41. 2
      test/ciao/search/no_params.coffee
  42. 2
      test/ciao/search/sources_invalid.coffee
  43. 2
      test/ciao/search/sources_layers_invalid_combo.coffee
  44. 2
      test/ciao/search/text_invalid.coffee

1
app.js

@ -26,7 +26,6 @@ v1.addRoutes(app, peliasConfig);
/** ----------------------- error middleware ----------------------- **/
app.use( require('./middleware/404') );
app.use( require('./middleware/408') );
app.use( require('./middleware/500') );
module.exports = app;

2
controller/place.js

@ -29,7 +29,7 @@ function setup( backend ){
// error handler
if( err ){
req.errors.push( err.message ? err.message : err );
req.errors.push( err );
}
// set response data
else {

2
controller/search.js

@ -31,7 +31,7 @@ function setup( backend, query ){
// error handler
if( err ){
req.errors.push( err.message ? err.message : err );
req.errors.push( err );
}
// set response data
else {

15
middleware/408.js

@ -1,15 +0,0 @@
// handle time out errors
function middleware(err, req, res, next) {
res.header('Cache-Control','public');
var error = (err && err.message) ? err.message : err;
if( res.statusCode === 408 || (error.toLowerCase().indexOf('request timeout') !== -1) ){
res.status(408);
res.json({ error: typeof error === 'string' ? error : 'request timeout' });
} else {
next(err);
}
}
module.exports = middleware;

35
middleware/sendJSON.js

@ -1,12 +1,43 @@
var check = require('check-types');
function sendJSONResponse(req, res, next) {
// do nothing if no result data set
if (!res || !res.body) {
if (!res || !check.object(res.body) || !check.object(res.body.geocoding)) {
return next();
}
// default status
var statusCode = 200;
// 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
statusCode = 400; // 400 Bad Request
// 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
}
}
});
}
// respond
return res.status(200).json(res.body);
return res.status(statusCode).json(res.body);
}
module.exports = sendJSONResponse;

2
test/ciao/autocomplete/focus_point_invalid_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete?text=a&focus.point.lat=foo&focus.point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/autocomplete/focus_point_invalid_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete?text=a&focus.point.lat=40.744243&focus.point.lon='
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/autocomplete/focus_point_missing_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete?text=a&focus.point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/autocomplete/focus_point_missing_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete?text=a&focus.point.lat=40.744243'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/autocomplete/no_params.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/autocomplete/text_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/autocomplete?text='
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/place/missing_id.coffee

@ -3,7 +3,7 @@
path: '/v1/place'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/boundary_circle_invalid_radius.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=40.744243&point.lon=-73.990342&boundary.circle.radius=foo'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/boundary_country_invalid_alpha2.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=1&boundary.country=ZZ'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/boundary_country_invalid_alpha3.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=1&boundary.country=ZZZ'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/boundary_country_invalid_iso3166.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=1&boundary.country=FOOBAR'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/duplicate_parameter_name.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=1&param=value1&param=value2'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/layers_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=2&layers=notlayer'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/layers_mix_invalid_valid.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=2&layers=country,notlayer'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/non_scalar_parameter.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=1&parameter[idx]=value'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/point_invalid_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=foo&point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/point_invalid_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=40.744243&point.lon='
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/point_missing_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/point_missing_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=40.744243'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/sources_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=2&sources=openstreetmap,notasource'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/reverse/sources_layers_invalid_combo.coffee

@ -3,7 +3,7 @@
path: '/v1/reverse?point.lat=1&point.lon=2&sources=quattroshapes&layers=address'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_circle_invalid_lat_lon_types.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.circle.lat=foo&boundary.circle.lon=bar'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_circle_invalid_radius.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.circle.lat=40.744243&boundary.circle.lon=-73.990342&boundary.circle.radius=foo'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_circle_missing_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.circle.lon=-73.990342&boundary.circle.radius=100'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_circle_missing_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.circle.lat=40.744243&boundary.circle.radius=100'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_country_invalid_alpha2.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.country=ZZ'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_country_invalid_alpha3.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.country=ZZZ'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_country_invalid_iso3166.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.country=FOOBAR'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/boundary_rect_partially_specified.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&boundary.rect.min_lat=-40.659'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/focus_point_invalid_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&focus.point.lat=foo&focus.point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/focus_point_invalid_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&focus.point.lat=40.744243&focus.point.lon='
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/focus_point_missing_lat.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&focus.point.lon=-73.990342'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/focus_point_missing_lon.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&focus.point.lat=40.744243'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/layers_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&layers=notlayer'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/layers_mix_invalid_valid.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&layers=country,notlayer'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/no_params.coffee

@ -3,7 +3,7 @@
path: '/v1/search'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/sources_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&sources=openstreetmap,notasource'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/sources_layers_invalid_combo.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text=a&sources=quattroshapes&layers=address'
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

2
test/ciao/search/text_invalid.coffee

@ -3,7 +3,7 @@
path: '/v1/search?text='
#? 200 ok
response.statusCode.should.be.equal 200
response.statusCode.should.be.equal 400
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'

Loading…
Cancel
Save