Don't remove existing unhandled exception and promise rejection handlers in Node

This commit is contained in:
Steve Gravrock
2025-09-14 14:49:55 -07:00
parent 2c6ce35ccc
commit 5439c8c9cd
3 changed files with 25 additions and 75 deletions

View File

@@ -4559,56 +4559,34 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
class NodeAdapter { class NodeAdapter {
#global; #global;
#dispatch; #dispatch;
#originalHandlers;
#jasmineHandlers;
constructor(global, dispatch) { constructor(global, dispatch) {
this.#global = global; this.#global = global;
this.#dispatch = dispatch; this.#dispatch = dispatch;
this.#jasmineHandlers = {};
this.#originalHandlers = {};
this.onError = this.onError.bind(this); this.onError = this.onError.bind(this);
this.onUnhandledRejection = this.onUnhandledRejection.bind(this); this.onUnhandledRejection = this.onUnhandledRejection.bind(this);
} }
install() { install() {
this.#installHandler('uncaughtException', this.onError); this.#global.process.on('uncaughtException', this.onError);
this.#installHandler('unhandledRejection', this.onUnhandledRejection); this.#global.process.on('unhandledRejection', this.onUnhandledRejection);
this.#installHandler( this.#global.process.on(
'rejectionHandled', 'rejectionHandled',
this.#dispatch.onRejectionHandled this.#dispatch.onRejectionHandled
); );
} }
uninstall() { uninstall() {
const errorTypes = Object.keys(this.#originalHandlers); this.#global.process.removeListener('uncaughtException', this.onError);
for (const errorType of errorTypes) { this.#global.process.removeListener(
this.#global.process.removeListener( 'unhandledRejection',
errorType, this.onUnhandledRejection
this.#jasmineHandlers[errorType] );
); this.#global.process.removeListener(
'rejectionHandled',
for (let i = 0; i < this.#originalHandlers[errorType].length; i++) { this.#dispatch.onRejectionHandled
this.#global.process.on(
errorType,
this.#originalHandlers[errorType][i]
);
}
delete this.#originalHandlers[errorType];
delete this.#jasmineHandlers[errorType];
}
}
#installHandler(errorType, handler) {
this.#originalHandlers[errorType] = this.#global.process.listeners(
errorType
); );
this.#jasmineHandlers[errorType] = handler;
this.#global.process.removeAllListeners(errorType);
this.#global.process.on(errorType, handler);
} }
#augmentError(error, isUnhandledRejection) { #augmentError(error, isUnhandledRejection) {

View File

@@ -142,11 +142,9 @@ describe('GlobalErrors', function() {
errors.install(); errors.install();
expect(globals.listeners.uncaughtException).toEqual([ expect(globals.listeners.uncaughtException).toEqual([
originalHandler,
jasmine.any(Function) jasmine.any(Function)
]); ]);
expect(globals.listeners.uncaughtException).not.toEqual([
originalHandler()
]);
errors.pushListener(handler); errors.pushListener(handler);
@@ -175,11 +173,9 @@ describe('GlobalErrors', function() {
errors.install(); errors.install();
expect(globals.listeners.unhandledRejection).toEqual([ expect(globals.listeners.unhandledRejection).toEqual([
originalHandler,
jasmine.any(Function) jasmine.any(Function)
]); ]);
expect(globals.listeners.unhandledRejection).not.toEqual([
originalHandler()
]);
errors.pushListener(handler); errors.pushListener(handler);
@@ -257,11 +253,9 @@ describe('GlobalErrors', function() {
errors.install(); errors.install();
expect(globals.listeners.rejectionHandled).toEqual([ expect(globals.listeners.rejectionHandled).toEqual([
originalHandler,
jasmine.any(Function) jasmine.any(Function)
]); ]);
expect(globals.listeners.rejectionHandled).not.toEqual([
originalHandler
]);
errors.uninstall(); errors.uninstall();
expect(globals.listeners.rejectionHandled).toEqual([originalHandler]); expect(globals.listeners.rejectionHandled).toEqual([originalHandler]);

View File

@@ -191,56 +191,34 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
class NodeAdapter { class NodeAdapter {
#global; #global;
#dispatch; #dispatch;
#originalHandlers;
#jasmineHandlers;
constructor(global, dispatch) { constructor(global, dispatch) {
this.#global = global; this.#global = global;
this.#dispatch = dispatch; this.#dispatch = dispatch;
this.#jasmineHandlers = {};
this.#originalHandlers = {};
this.onError = this.onError.bind(this); this.onError = this.onError.bind(this);
this.onUnhandledRejection = this.onUnhandledRejection.bind(this); this.onUnhandledRejection = this.onUnhandledRejection.bind(this);
} }
install() { install() {
this.#installHandler('uncaughtException', this.onError); this.#global.process.on('uncaughtException', this.onError);
this.#installHandler('unhandledRejection', this.onUnhandledRejection); this.#global.process.on('unhandledRejection', this.onUnhandledRejection);
this.#installHandler( this.#global.process.on(
'rejectionHandled', 'rejectionHandled',
this.#dispatch.onRejectionHandled this.#dispatch.onRejectionHandled
); );
} }
uninstall() { uninstall() {
const errorTypes = Object.keys(this.#originalHandlers); this.#global.process.removeListener('uncaughtException', this.onError);
for (const errorType of errorTypes) { this.#global.process.removeListener(
this.#global.process.removeListener( 'unhandledRejection',
errorType, this.onUnhandledRejection
this.#jasmineHandlers[errorType] );
); this.#global.process.removeListener(
'rejectionHandled',
for (let i = 0; i < this.#originalHandlers[errorType].length; i++) { this.#dispatch.onRejectionHandled
this.#global.process.on(
errorType,
this.#originalHandlers[errorType][i]
);
}
delete this.#originalHandlers[errorType];
delete this.#jasmineHandlers[errorType];
}
}
#installHandler(errorType, handler) {
this.#originalHandlers[errorType] = this.#global.process.listeners(
errorType
); );
this.#jasmineHandlers[errorType] = handler;
this.#global.process.removeAllListeners(errorType);
this.#global.process.on(errorType, handler);
} }
#augmentError(error, isUnhandledRejection) { #augmentError(error, isUnhandledRejection) {