Skip to content

Commit f80a221

Browse files
committed
fixup! repl: remove dependency on domain module
- Mark addUncaughtExceptionCaptureCallback as experimental - Add changes entry to setUncaughtExceptionCaptureCallback docs - Use validateFunction instead of manual type check
1 parent 5e0f5a3 commit f80a221

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

doc/api/process.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ This feature is not available in [`Worker`][] threads.
742742
added: REPLACEME
743743
-->
744744

745+
> Stability: 1 - Experimental
746+
745747
* `fn` {Function}
746748

747749
The `process.addUncaughtExceptionCaptureCallback()` function adds a callback
@@ -4057,6 +4059,11 @@ This implies calling `module.setSourceMapsSupport()` with an option
40574059
40584060
<!-- YAML
40594061
added: v9.3.0
4062+
changes:
4063+
- version: REPLACEME
4064+
pr-url: https://github.com/nodejs/node/pull/61227
4065+
description: Use `process.addUncaughtExceptionCaptureCallback()` to
4066+
register multiple callbacks.
40604067
-->
40614068
40624069
* `fn` {Function|null}

lib/internal/process/execution.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET,
1919
},
2020
} = require('internal/errors');
21+
const { validateFunction } = require('internal/validators');
2122
const { pathToFileURL } = require('internal/url');
2223
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
2324
const {
@@ -137,9 +138,7 @@ function setUncaughtExceptionCaptureCallback(fn) {
137138
// Auxiliary callbacks are called first; if any returns true, the error is handled.
138139
// Otherwise, the primary callback (if set) is called.
139140
function addUncaughtExceptionCaptureCallback(fn) {
140-
if (typeof fn !== 'function') {
141-
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
142-
}
141+
validateFunction(fn, 'fn');
143142
if (exceptionHandlerState.auxiliaryCallbacks.length === 0 &&
144143
exceptionHandlerState.captureFn === null) {
145144
exceptionHandlerState.reportFlag =

lib/repl.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,16 @@ const kBufferedCommandSymbol = Symbol('bufferedCommand');
209209
const kLoadingSymbol = Symbol('loading');
210210

211211
function processNewListener(event, listener) {
212-
if (event === 'uncaughtException' &&
213-
process.domain &&
214-
listener.name !== 'domainUncaughtExceptionClear' &&
215-
domainSet.has(process.domain)) {
216-
// Throw an error so that the event will not be added and the current
217-
// domain takes over. That way the user is notified about the error
218-
// and the current code evaluation is stopped, just as any other code
219-
// that contains an error.
220-
throw new ERR_INVALID_REPL_INPUT(
221-
'Listeners for `uncaughtException` cannot be used in the REPL');
212+
if (event === 'uncaughtException') {
213+
const store = replContext.getStore();
214+
if (store?.replServer) {
215+
// Throw an error so that the event will not be added and the
216+
// current REPL handles it. That way the user is notified about
217+
// the error and the current code evaluation is stopped, just as
218+
// any other code that contains an error.
219+
throw new ERR_INVALID_REPL_INPUT(
220+
'Listeners for `uncaughtException` cannot be used in the REPL');
221+
}
222222
}
223223
}
224224

@@ -396,24 +396,9 @@ class REPLServer extends Interface {
396396
// It is possible to introspect the running REPL accessing this variable
397397
// from inside the REPL. This is useful for anyone working on the REPL.
398398
module.exports.repl = this;
399-
} else if (!addedNewListener) {
400-
// Add this listener only once. Otherwise we'd have to add a single
401-
// listener to each REPL instance and that could trigger the
402-
// `MaxListenersExceededWarning`.
403-
process.prependListener('newListener', (event, listener) => {
404-
if (event === 'uncaughtException') {
405-
const store = replContext.getStore();
406-
if (store?.replServer) {
407-
// Throw an error so that the event will not be added and the
408-
// current REPL handles it. That way the user is notified about
409-
// the error and the current code evaluation is stopped, just as
410-
// any other code that contains an error.
411-
throw new ERR_INVALID_REPL_INPUT(
412-
'Listeners for `uncaughtException` cannot be used in the REPL');
413-
}
414-
}
415-
});
416-
addedNewListener = true;
399+
} else {
400+
addProcessNewListener();
401+
this.once('exit', removeProcessNewListener);
417402
}
418403

419404
// Set up exception capture for async error handling

0 commit comments

Comments
 (0)