I have been reading about Node exception handling for hours. I understand the disadvantages of using uncaughtException , I understand that stopping 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 correctly implement domains, in particular explicit binding ...
... but I'm still not getting any results just for basic error handling.
I would like to be able to simply catch any uncaught exceptions for the purpose of registration. I do not mind killing a process or anything else that is considered "unwanted." I just want a log.
I donβt feel that I would have to wrap everything in try / catch or use some kind of library for emit errors ... please correct me if I am wrong and I will change my ways.
I use 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();
The error is displayed in the console, but the console never receives the message "DOMAIN SERVER ERROR ...". I also tried to wrap the request / response in my own domain, but to no avail. Even more disappointing is the fact that using the following also does not work:
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?
Ryan wheale
source share