Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created August 6, 2010 19:20
Show Gist options
  • Save indexzero/511834 to your computer and use it in GitHub Desktop.
Save indexzero/511834 to your computer and use it in GitHub Desktop.
var sys = require('sys'),
http = require('http');
process.on('uncaughtException', function (err) {
sys.puts('Caught exception: ' + err);
});
// Simple Example
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Throwing exception')
res.end();
throw new Error();
}).listen(8080);
// Type Error
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Throwing exception')
res.end();
var myObj = {};
myObj.doesntExist();
}).listen(8081);
@indexzero
Copy link
Author

Hmmm.... you're right. But the uncaughtException event is all about your request / response then why not just:

http.createServer(function (req, res) {
  try {
    // Entry point for all your server logic
  } 
  catch (err) {
    // Respond to client based on error
  }
});

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