|
29 | 29 | const util = require('util'); |
30 | 30 | const EventEmitter = require('events'); |
31 | 31 | const { |
32 | | - ERR_DOMAIN_CALLBACK_NOT_AVAILABLE, |
33 | | - ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE, |
34 | 32 | ERR_UNHANDLED_ERROR |
35 | 33 | } = require('internal/errors').codes; |
36 | 34 | const { createHook } = require('async_hooks'); |
37 | 35 |
|
| 36 | +const CAPTURE_CB_KEY = Symbol('domain'); |
| 37 | + |
38 | 38 | // overwrite process.domain with a getter/setter that will allow for more |
39 | 39 | // effective optimizations |
40 | 40 | var _domain = [null]; |
@@ -74,23 +74,7 @@ const asyncHook = createHook({ |
74 | 74 | } |
75 | 75 | }); |
76 | 76 |
|
77 | | -// When domains are in use, they claim full ownership of the |
78 | | -// uncaught exception capture callback. |
79 | | -if (process.hasUncaughtExceptionCaptureCallback()) { |
80 | | - throw new ERR_DOMAIN_CALLBACK_NOT_AVAILABLE(); |
81 | | -} |
82 | | - |
83 | | -// Get the stack trace at the point where `domain` was required. |
84 | | -// eslint-disable-next-line no-restricted-syntax |
85 | | -const domainRequireStack = new Error('require(`domain`) at this point').stack; |
86 | | - |
87 | 77 | const { setUncaughtExceptionCaptureCallback } = process; |
88 | | -process.setUncaughtExceptionCaptureCallback = function(fn) { |
89 | | - const err = new ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE(); |
90 | | - err.stack = err.stack + '\n' + '-'.repeat(40) + '\n' + domainRequireStack; |
91 | | - throw err; |
92 | | -}; |
93 | | - |
94 | 78 |
|
95 | 79 | let sendMakeCallbackDeprecation = false; |
96 | 80 | function emitMakeCallbackDeprecation() { |
@@ -125,10 +109,10 @@ internalBinding('domain').enable(topLevelDomainCallback); |
125 | 109 |
|
126 | 110 | function updateExceptionCapture() { |
127 | 111 | if (stack.every((domain) => domain.listenerCount('error') === 0)) { |
128 | | - setUncaughtExceptionCaptureCallback(null); |
| 112 | + setUncaughtExceptionCaptureCallback(CAPTURE_CB_KEY, null); |
129 | 113 | } else { |
130 | | - setUncaughtExceptionCaptureCallback(null); |
131 | | - setUncaughtExceptionCaptureCallback((er) => { |
| 114 | + setUncaughtExceptionCaptureCallback(CAPTURE_CB_KEY, null); |
| 115 | + setUncaughtExceptionCaptureCallback(CAPTURE_CB_KEY, (er) => { |
132 | 116 | return process.domain._errorHandler(er); |
133 | 117 | }); |
134 | 118 | } |
@@ -211,7 +195,7 @@ Domain.prototype._errorHandler = function(er) { |
211 | 195 | // Clear the uncaughtExceptionCaptureCallback so that we know that, even |
212 | 196 | // if technically the top-level domain is still active, it would |
213 | 197 | // be ok to abort on an uncaught exception at this point |
214 | | - setUncaughtExceptionCaptureCallback(null); |
| 198 | + setUncaughtExceptionCaptureCallback(CAPTURE_CB_KEY, null); |
215 | 199 | try { |
216 | 200 | caught = this.emit('error', er); |
217 | 201 | } finally { |
|
0 commit comments