Javascript 在node.js域中,如何将异常抛出到顶层?
在node.js中,Javascript 在node.js域中,如何将异常抛出到顶层?,javascript,node.js,exception,Javascript,Node.js,Exception,在node.js中,process.domain在顶层为空。这对我来说意味着没有可以抛出错误的“顶级域”。这就引出了我的问题: var d = require('domain').create(); d.on('error', function(er) { console.log(er) }) d.run(function() { setTimeout(function(){ throw new Error("?") // how do you throw thi
process.domain
在顶层为空。这对我来说意味着没有可以抛出错误的“顶级域”。这就引出了我的问题:
var d = require('domain').create();
d.on('error', function(er) {
console.log(er)
})
d.run(function() {
setTimeout(function(){
throw new Error("?") // how do you throw this such that it doesn't hit the on 'error' handler but instead is treated like the error below would be?
}, 0)
})
//throw new Error("the kind of error handling i want")
基本上,我尝试在某一点存储process.domain,然后在域上下文发生更改时在该域的上下文中抛出异常。问题是,因为没有“顶级域”,所以我不清楚如何模拟在顶级通常如何处理异常
这是一种方式,但我不喜欢它,因为它依赖于我认为是一个很差的领域设计:
var d = require('domain').create();
d.on('error', function(er) {
console.log("never gets here, oddly enough")
})
d.run(function() {
var d1 = require('domain').create()
d1.on('error', function(e) {
throw e
})
d1.run(function() {
setTimeout(function() {
throw new Error("error I want to throw at the top-level")
})
})
})
你看了吗?是的,事实上,我发布了一个关于这个问题的答案。