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 {
#global;
#dispatch;
#originalHandlers;
#jasmineHandlers;
constructor(global, dispatch) {
this.#global = global;
this.#dispatch = dispatch;
this.#jasmineHandlers = {};
this.#originalHandlers = {};
this.onError = this.onError.bind(this);
this.onUnhandledRejection = this.onUnhandledRejection.bind(this);
}
install() {
this.#installHandler('uncaughtException', this.onError);
this.#installHandler('unhandledRejection', this.onUnhandledRejection);
this.#installHandler(
this.#global.process.on('uncaughtException', this.onError);
this.#global.process.on('unhandledRejection', this.onUnhandledRejection);
this.#global.process.on(
'rejectionHandled',
this.#dispatch.onRejectionHandled
);
}
uninstall() {
const errorTypes = Object.keys(this.#originalHandlers);
for (const errorType of errorTypes) {
this.#global.process.removeListener(
errorType,
this.#jasmineHandlers[errorType]
);
for (let i = 0; i < this.#originalHandlers[errorType].length; i++) {
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.#global.process.removeListener('uncaughtException', this.onError);
this.#global.process.removeListener(
'unhandledRejection',
this.onUnhandledRejection
);
this.#global.process.removeListener(
'rejectionHandled',
this.#dispatch.onRejectionHandled
);
this.#jasmineHandlers[errorType] = handler;
this.#global.process.removeAllListeners(errorType);
this.#global.process.on(errorType, handler);
}
#augmentError(error, isUnhandledRejection) {

View File

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

View File

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