Unify error dispatching between browser and node

This commit is contained in:
Steve Gravrock
2025-07-11 07:54:33 -07:00
parent d53d2ff3eb
commit ff476b1982
3 changed files with 27 additions and 50 deletions

View File

@@ -4321,14 +4321,13 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
this.#onBrowserError = event =>
this._dispatchBrowserError(event.error, event);
this.#onBrowserRejection = event => this._browserRejectionHandler(event);
this.#onBrowserError = event => this.#dispatchError(event.error, event);
this.#onBrowserRejection = event => this.#browserRejectionHandler(event);
this.#onNodeError = error =>
this._handleNodeEvent(error, 'uncaughtException', 'Uncaught exception');
this.#handleNodeEvent(error, 'uncaughtException', 'Uncaught exception');
this.#onNodeRejection = error =>
this._handleNodeEvent(
this.#handleNodeEvent(
error,
'unhandledRejection',
'Unhandled promise rejection'
@@ -4344,8 +4343,8 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this._installNodeHandler('uncaughtException', this.#onNodeError);
this._installNodeHandler('unhandledRejection', this.#onNodeRejection);
this.#installNodeHandler('uncaughtException', this.#onNodeError);
this.#installNodeHandler('unhandledRejection', this.#onNodeRejection);
} else {
this.#global.addEventListener('error', this.#onBrowserError);
@@ -4362,9 +4361,9 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this._nodeUninstall();
this.#nodeUninstall();
} else {
this._browserUninstall();
this.#browserUninstall();
}
}
@@ -4405,7 +4404,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#onRemoveOverrideHandler = null;
}
_nodeUninstall() {
#nodeUninstall() {
const errorTypes = Object.keys(this.#originalHandlers);
for (const errorType of errorTypes) {
this.#global.process.removeListener(
@@ -4424,7 +4423,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
}
}
_browserUninstall() {
#browserUninstall() {
this.#global.removeEventListener('error', this.#onBrowserError);
this.#global.removeEventListener(
'unhandledrejection',
@@ -4432,7 +4431,8 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
);
}
_dispatchBrowserError(error, event) {
// Either error or event may be undefined
#dispatchError(error, event) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
this.#overrideHandler(error);
@@ -4448,20 +4448,20 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
}
}
_browserRejectionHandler(event) {
#browserRejectionHandler(event) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
this._dispatchBrowserError(event.reason, event);
this.#dispatchError(event.reason, event);
} else {
this._dispatchBrowserError(
this.#dispatchError(
'Unhandled promise rejection: ' + event.reason,
event
);
}
}
_installNodeHandler(errorType, handler) {
#installNodeHandler(errorType, handler) {
this.#originalHandlers[errorType] = this.#global.process.listeners(
errorType
);
@@ -4471,7 +4471,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#global.process.on(errorType, handler);
}
_handleNodeEvent(error, errorType, jasmineMessage) {
#handleNodeEvent(error, errorType, jasmineMessage) {
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
} else {
@@ -4495,19 +4495,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
error = new Error(substituteMsg);
}
const handler = this.#handlers[this.#handlers.length - 1];
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
this.#overrideHandler(error);
return;
}
if (handler) {
handler(error);
} else {
throw error;
}
this.#dispatchError(error);
}
}

View File

@@ -131,7 +131,7 @@ describe('GlobalErrors', function() {
dispatchEvent(globals.listeners, 'uncaughtException', new Error('bar'));
expect(handler).toHaveBeenCalledWith(new Error('bar'));
expect(handler).toHaveBeenCalledWith(new Error('bar'), undefined);
expect(handler.calls.argsFor(0)[0].jasmineMessage).toBe(
'Uncaught exception: Error: bar'
);
@@ -186,7 +186,8 @@ describe('GlobalErrors', function() {
'Unhandled promise rejection: 17\n' +
'(Tip: to get a useful stack trace, use ' +
'Promise.reject(new Error(...)) instead of Promise.reject(...).)'
)
),
undefined
);
});

View File

@@ -18,8 +18,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
this.#onBrowserError = event =>
this.#dispatchBrowserError(event.error, event);
this.#onBrowserError = event => this.#dispatchError(event.error, event);
this.#onBrowserRejection = event => this.#browserRejectionHandler(event);
this.#onNodeError = error =>
@@ -129,7 +128,8 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
);
}
#dispatchBrowserError(error, event) {
// Either error or event may be undefined
#dispatchError(error, event) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
this.#overrideHandler(error);
@@ -149,9 +149,9 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
this.#dispatchBrowserError(event.reason, event);
this.#dispatchError(event.reason, event);
} else {
this.#dispatchBrowserError(
this.#dispatchError(
'Unhandled promise rejection: ' + event.reason,
event
);
@@ -192,19 +192,7 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
error = new Error(substituteMsg);
}
const handler = this.#handlers[this.#handlers.length - 1];
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
this.#overrideHandler(error);
return;
}
if (handler) {
handler(error);
} else {
throw error;
}
this.#dispatchError(error);
}
}