Domain.prototype.enter = function() {
if (this._disposed) return;
// note that this might be a no-op, but we still need
// to push it onto the stack so that we can pop it later.
exports.active = process.domain = this;
stack.push(this);
_domain_flag[0] = stack.length;
};
Domain.prototype.exit = function() {
// skip disposed domains, as usual, but also don't do anything if this
// domain is not on the stack.
var index = stack.lastIndexOf(this);
if (this._disposed || index === -1) return;
// exit all domains until this one.
stack.splice(index);
_domain_flag[0] = stack.length;
exports.active = stack[stack.length - 1];
process.domain = exports.active;
};
process._fatalException = function(er) {
var caught;
if (process.domain && process.domain._errorHandler)
caught = process.domain._errorHandler(er) || caught;
if (!caught)
caught = process.emit('uncaughtException', er);
// If someone handled it, then great. otherwise, die in C++ land
// since that means that we'll exit the process, emit the 'exit' event
if (!caught) {
try {
if (!process._exiting) {
process._exiting = true;
process.emit('exit', 1);
}
} catch (er) {
// nothing to be done about it at this point.
}
// if we handled an error, then make sure any ticks get processed
} else {
NativeModule.require('timers').setImmediate(process._tickCallback);
}
return caught;
};
如果当前 process 使用了 domain, 也是就 process.domain 不为空,就调用 _errorHandler 来处理, 当前也存在没有处理的情况,职责链来到 process, process 则触发 uncaughtException 事件。