I have been reading for hours on exception handling in Node. I understand the cons of using uncaughtException
, I understand that shutting down the process is good for preventing any "unknown state" where "anything can happen". I understand that using domains is the way to go, and I understand how to properly implement domains, specifically Explicit Binding...
...but I'm still not getting any results for just basic error handling.
I would like to be able to just catch any uncaught exceptions for the purpose of logging. I don't mind killing the process or anything else deemed "undesirable". I just want a log.
I don't feel like I should have to wrap everything in a try/catch or use some library to emit
errors... please correct me if I'm wrong and I will change my ways.
I am using Node and Express and I have the following simple code:
var express = require('express');
var domain = require('domain');
var serverDomain = domain.create();
serverDomain.on('error', function(err) {
console.log("SERVER DOMAIN ERROR: " + err.message);
});
serverDomain.run(function() {
var app = express();
app.get('/testing', function() {
app.nonExistent.call(); // this throws an error
});
var server = app.listen(8000, function() {
console.log('Listening on port %d', server.address().port);
});
});
The error shows up in the console, but the console never receives the "SERVER DOMAIN ERROR..." message. I have also tried wrapping the request/response in their own domain as well, to no avail. Even more disappointing is the fact that using the following does not work either:
process.on('uncaughtException', function(err) {
console.log('uncaughtException caught the error');
});
Am I doing something wrong? Where do I go from here? How can I catch the above error?