Handling connect.timeout Error #721

Closed
ajostergaard opened this Issue Jan 6, 2013 · 6 comments

4 participants

@ajostergaard

"The timeout error is passed to next() so that you may customize the response behaviour."

This doesn't seem to be working for me - perhaps because the Error is coming from an async call?!? :-S

@tj
Sencha Labs member
tj commented Jan 9, 2013

yeah that's a bit of an unresolved problem since it's out of the middleware flow, for that sort of error handling we'd pretty much have no choice but to canonicalize a more specific error mechanism like app.error(fn) and not use middleware, might be best to remove this middleware for now

@dougwilson

@ajostergaard That documentation text is referring to your ability to "handle" the timeout error within an error middleware (app.use(function (err, req, res, next) { ... })); standard middleware will never be aware the timeout was triggered unless it is listening for the timeout event on res.

@ajostergaard

@dougwilson - are you able to handle the error in your error middleware? It doesn't work for me. I think it's because the Error is being raised from an async call so jumps out of all the middleware (err middleware included) and ends up as an uncaughtException. I've worked around it by deploying my own timeout which handles right in req.on('timeout' ... No use as a solution for anybody else though.

@dougwilson

It should work as long as your timeout is under 2 minutes. This is taken from the timeout middleware test and works fine for me:

var app = connect()
  .use(connect.timeout(300))
  .use(function(req, res){
    setTimeout(function(){
      res.end('Hello');
    }, 400);
  })
  .use(function(err, req, res, next){
    res.statusCode = err.status;
    res.end('timeout of ' + err.timeout + 'ms exceeded');
  });
@ajostergaard

@dougwilson - thanks for that! Turns out my issue was because I was using express and not pulling in the router before the error handlers. This works for Express:

var app = express()
.use(express.timeout(300));

app.use(app.router)
.use(function(err, req, res, next){
res.statusCode = err.status;
res.end('timeout of ' + err.timeout + 'ms exceeded');
})
.get('/', function(req, res){
setTimeout(function(){
res.end('Hello');
}, 400);
});

I guess the issue @visionmedia is talking about is different? Perhaps there's an issue catching un-handled errors in async calls but my issue above wasn't an example of one?

@jonathanong

so this isn't a timeout issue, an error handling issue! but yeah, timeout isn't really robust or anything, especially if you have a lot of async code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment