Node, Express, domains, uncaught exceptions - still lost

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(); // this throws an error }); var server = app.listen(8000, function() { console.log('Listening on port %d', server.address().port); }); }); 

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?

+9
javascript error-handling express node.js-domains
source share
2 answers

You can use connect-domain .

The problem is that the exception occurs during Connect routing, which has both try / catch blocks around its execution, as well as a default error handler that displays data about the stack trace when working in non-production mode. Since the exception is handled internally by Express, it never reaches your external level for handling domains.

Here is an example of using the domain-domain package instead of the domain.

http://masashi-k.blogspot.com/2012/12/express3-global-error-handling-domain.html

 var express = require('express'); var connectDomain = require('connect-domain'); var app = express(); app.use(connectDomain()); app.get('/testing', function() { app.nonExistent.call(); // this throws an error }); app.use(function(err, req, res, next) { res.end(err.message); // this catches the error!! }); var server = app.listen(8000, function() { console.log('Listening on port %d', server.address().port); }); 
+5
source share

This is because Express itself handles errors that may occur, and thus simplifies your work. See Express Error Handling Guide . You should use the structure below to handle errors that may occur:

 app.use(function(err, req, res, next){ console.error(err.stack); res.send(500, 'Something broke!'); }); 
+5
source share

All Articles