Skip to content

Instantly share code, notes, and snippets.

@benw
Created January 20, 2010 23:18
Show Gist options
  • Save benw/282399 to your computer and use it in GitHub Desktop.
Save benw/282399 to your computer and use it in GitHub Desktop.
http.createServer(dispatchRequest).listen(port);
function dispatchRequest(req, res)
{
res.sentinel = new Sentinel(function (e) {
// Like a catch() clause that catches callbacks too.
// Called if handleRequest throws, or if any of the callbacks
// registered by handleRequest throw, or if any of the
// callbacks registered by those callbacks throw, etc.
res.sendHeader(500, headers);
res.sendBody('500 Internal Server Error\n\n' + e.stack);
}, function () {
// Like a finally clause that runs after all the callbacks,
// or after the "catch" callback above.
// Just in case handleRequest didn't manage to arrange
// for res.finish() to be called. Assumes that
// calling res.finish() twice has no bad effects.
res.finish();
});
handleRequest(req, res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment