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

View File

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

View File

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