Browse Source

Merge pull request #163 from pelias/err-fix

Error handling bug fix
pull/168/merge
Harish Krishna 9 years ago
parent
commit
6efe46b796
  1. 6
      middleware/408.js
  2. 4
      middleware/500.js

6
middleware/408.js

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

4
middleware/500.js

@ -4,8 +4,10 @@ var logger = require( 'pelias-logger' ).get( 'middleware-500' );
function middleware(err, req, res, next) { function middleware(err, req, res, next) {
logger.error( 'Error: `%s`. Stack trace: `%s`.', err, err.stack ); logger.error( 'Error: `%s`. Stack trace: `%s`.', err, err.stack );
res.header('Cache-Control','no-cache'); res.header('Cache-Control','no-cache');
var error = (err && err.message) ? err.message : err;
if( res.statusCode < 400 ){ res.status(500); } if( res.statusCode < 400 ){ res.status(500); }
res.json({ error: err && typeof err.message === 'string' ? err.message : 'internal server error' }); res.json({ error: typeof error === 'string' ? error : 'internal server error' });
} }
module.exports = middleware; module.exports = middleware;

Loading…
Cancel
Save