admin管理员组文章数量:1314000
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?
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?
Share Improve this question edited May 24, 2019 at 17:30 Josh Lee 178k39 gold badges277 silver badges281 bronze badges asked May 27, 2014 at 6:52 Ryan WhealeRyan Wheale 28.4k10 gold badges86 silver badges102 bronze badges2 Answers
Reset to default 5You 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./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);
});
This happens because Express handles by itself the errors that may appear and in this way it simplifies your work. See Express Guide for error handling. You should use the structure below to handle the errors that may appear:
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
本文标签:
版权声明:本文标题:javascript - Node, Express, domains, uncaught exceptions - still lost - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740901256a2304514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论