Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  37] [ 2]  / answers: 1 / hits: 7310  / 10 Years ago, tue, may 27, 2014, 12:00:00

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?


More From » node.js

 Answers
2

You can use connect-domain.



The problem is that the exception happens during Connect's routing, which has both a try/catch block around its execution, as well as a default error handler which prints out stack trace details when running in a non-production mode. Since the exception is handled inside of Express, it never reaches your outer layer for the domains to handle.



Here is an example why to use connect-domain package instead of 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);
});

[#45020] Sunday, May 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaisep

Total Points: 748
Total Questions: 95
Total Answers: 108

Location: Federated States of Micronesia
Member since Sun, May 16, 2021
3 Years ago
blaisep questions
Wed, Dec 16, 20, 00:00, 4 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
Tue, Nov 12, 19, 00:00, 5 Years ago
Mon, Nov 11, 19, 00:00, 5 Years ago
;