Browse Source

Merge pull request #159 from pelias/408

Adding 408: request timeout response
pull/161/head
Harish Krishna 10 years ago
parent
commit
8b60fe4e9a
  1. 1
      app.js
  2. 13
      middleware/408.js
  3. 2
      middleware/500.js

1
app.js

@ -52,6 +52,7 @@ app.get( '/reverse', sanitisers.reverse.middleware, controllers.search(undefined
/** ----------------------- error middleware ----------------------- **/
app.use( require('./middleware/404') );
app.use( require('./middleware/408') );
app.use( require('./middleware/500') );
module.exports = app;

13
middleware/408.js

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

2
middleware/500.js

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

Loading…
Cancel
Save