Convert GlobalErrors to an ES6 class

This commit is contained in:
Steve Gravrock
2025-07-11 06:49:09 -07:00
parent adfbd00c75
commit d53d2ff3eb
3 changed files with 359 additions and 275 deletions

View File

@@ -22,7 +22,9 @@ export default defineConfig([{
...globals.node,
},
ecmaVersion: 2018,
// 2022 isn't exactly right, but it's the earliest version that allows
// private properties.
ecmaVersion: 2022,
sourceType: "commonjs",
},

View File

@@ -4302,25 +4302,144 @@ getJasmineRequireObj().formatErrorMsg = function() {
};
getJasmineRequireObj().GlobalErrors = function(j$) {
function GlobalErrors(global) {
global = global || j$.getGlobal();
class GlobalErrors {
#global;
#handlers;
#originalHandlers;
#jasmineHandlers;
#overrideHandler;
#onRemoveOverrideHandler;
#onBrowserError;
#onBrowserRejection;
#onNodeError;
#onNodeRejection;
const handlers = [];
let overrideHandler = null,
onRemoveOverrideHandler = null;
constructor(global) {
this.#global = global || j$.getGlobal();
function onBrowserError(event) {
dispatchBrowserError(event.error, event);
this.#handlers = [];
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
this.#onBrowserError = event =>
this._dispatchBrowserError(event.error, event);
this.#onBrowserRejection = event => this._browserRejectionHandler(event);
this.#onNodeError = error =>
this._handleNodeEvent(error, 'uncaughtException', 'Uncaught exception');
this.#onNodeRejection = error =>
this._handleNodeEvent(
error,
'unhandledRejection',
'Unhandled promise rejection'
);
this.#originalHandlers = {};
this.#jasmineHandlers = {};
}
function dispatchBrowserError(error, event) {
if (overrideHandler) {
install() {
if (
this.#global.process &&
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this._installNodeHandler('uncaughtException', this.#onNodeError);
this._installNodeHandler('unhandledRejection', this.#onNodeRejection);
} else {
this.#global.addEventListener('error', this.#onBrowserError);
this.#global.addEventListener(
'unhandledrejection',
this.#onBrowserRejection
);
}
}
uninstall() {
if (
this.#global.process &&
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this._nodeUninstall();
} else {
this._browserUninstall();
}
}
// The listener at the top of the stack will be called with two arguments:
// the error and the event. Either of them may be falsy.
// The error will normally be provided, but will be falsy in the case of
// some browser load-time errors. The event will normally be provided in
// browsers but will be falsy in Node.
// Listeners that are pushed after spec files have been loaded should be
// able to just use the error parameter.
pushListener(listener) {
this.#handlers.push(listener);
}
popListener(listener) {
if (!listener) {
throw new Error('popListener expects a listener');
}
this.#handlers.pop();
}
setOverrideListener(listener, onRemove) {
if (this.#overrideHandler) {
throw new Error("Can't set more than one override listener at a time");
}
this.#overrideHandler = listener;
this.#onRemoveOverrideHandler = onRemove;
}
removeOverrideListener() {
if (this.#onRemoveOverrideHandler) {
this.#onRemoveOverrideHandler();
}
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
}
_nodeUninstall() {
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];
}
}
_browserUninstall() {
this.#global.removeEventListener('error', this.#onBrowserError);
this.#global.removeEventListener(
'unhandledrejection',
this.#onBrowserRejection
);
}
_dispatchBrowserError(error, event) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
overrideHandler(error);
this.#overrideHandler(error);
return;
}
const handler = handlers[handlers.length - 1];
const handler = this.#handlers[this.#handlers.length - 1];
if (handler) {
handler(error, event);
@@ -4329,10 +4448,30 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
}
}
this.originalHandlers = {};
this.jasmineHandlers = {};
this.installOne_ = function installOne_(errorType, jasmineMessage) {
function taggedOnError(error) {
_browserRejectionHandler(event) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
this._dispatchBrowserError(event.reason, event);
} else {
this._dispatchBrowserError(
'Unhandled promise rejection: ' + event.reason,
event
);
}
}
_installNodeHandler(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);
}
_handleNodeEvent(error, errorType, jasmineMessage) {
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
} else {
@@ -4356,11 +4495,11 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
error = new Error(substituteMsg);
}
const handler = handlers[handlers.length - 1];
const handler = this.#handlers[this.#handlers.length - 1];
if (overrideHandler) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
overrideHandler(error);
this.#overrideHandler(error);
return;
}
@@ -4370,104 +4509,6 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
throw error;
}
}
this.originalHandlers[errorType] = global.process.listeners(errorType);
this.jasmineHandlers[errorType] = taggedOnError;
global.process.removeAllListeners(errorType);
global.process.on(errorType, taggedOnError);
this.uninstall = function uninstall() {
const errorTypes = Object.keys(this.originalHandlers);
for (const errorType of errorTypes) {
global.process.removeListener(
errorType,
this.jasmineHandlers[errorType]
);
for (let i = 0; i < this.originalHandlers[errorType].length; i++) {
global.process.on(errorType, this.originalHandlers[errorType][i]);
}
delete this.originalHandlers[errorType];
delete this.jasmineHandlers[errorType];
}
};
};
this.install = function install() {
if (
global.process &&
global.process.listeners &&
j$.isFunction_(global.process.on)
) {
this.installOne_('uncaughtException', 'Uncaught exception');
this.installOne_('unhandledRejection', 'Unhandled promise rejection');
} else {
global.addEventListener('error', onBrowserError);
const browserRejectionHandler = function browserRejectionHandler(
event
) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
dispatchBrowserError(event.reason, event);
} else {
dispatchBrowserError(
'Unhandled promise rejection: ' + event.reason,
event
);
}
};
global.addEventListener('unhandledrejection', browserRejectionHandler);
this.uninstall = function uninstall() {
global.removeEventListener('error', onBrowserError);
global.removeEventListener(
'unhandledrejection',
browserRejectionHandler
);
};
}
};
// The listener at the top of the stack will be called with two arguments:
// the error and the event. Either of them may be falsy.
// The error will normally be provided, but will be falsy in the case of
// some browser load-time errors. The event will normally be provided in
// browsers but will be falsy in Node.
// Listeners that are pushed after spec files have been loaded should be
// able to just use the error parameter.
this.pushListener = function pushListener(listener) {
handlers.push(listener);
};
this.popListener = function popListener(listener) {
if (!listener) {
throw new Error('popListener expects a listener');
}
handlers.pop();
};
this.setOverrideListener = function(listener, onRemove) {
if (overrideHandler) {
throw new Error("Can't set more than one override listener at a time");
}
overrideHandler = listener;
onRemoveOverrideHandler = onRemove;
};
this.removeOverrideListener = function() {
if (onRemoveOverrideHandler) {
onRemoveOverrideHandler();
}
overrideHandler = null;
onRemoveOverrideHandler = null;
};
}
return GlobalErrors;

View File

@@ -1,23 +1,142 @@
getJasmineRequireObj().GlobalErrors = function(j$) {
function GlobalErrors(global) {
global = global || j$.getGlobal();
class GlobalErrors {
#global;
#handlers;
#originalHandlers;
#jasmineHandlers;
#overrideHandler;
#onRemoveOverrideHandler;
#onBrowserError;
#onBrowserRejection;
#onNodeError;
#onNodeRejection;
const handlers = [];
let overrideHandler = null,
onRemoveOverrideHandler = null;
constructor(global) {
this.#global = global || j$.getGlobal();
function onBrowserError(event) {
dispatchBrowserError(event.error, event);
this.#handlers = [];
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
this.#onBrowserError = event =>
this.#dispatchBrowserError(event.error, event);
this.#onBrowserRejection = event => this.#browserRejectionHandler(event);
this.#onNodeError = error =>
this.#handleNodeEvent(error, 'uncaughtException', 'Uncaught exception');
this.#onNodeRejection = error =>
this.#handleNodeEvent(
error,
'unhandledRejection',
'Unhandled promise rejection'
);
this.#originalHandlers = {};
this.#jasmineHandlers = {};
}
function dispatchBrowserError(error, event) {
if (overrideHandler) {
install() {
if (
this.#global.process &&
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this.#installNodeHandler('uncaughtException', this.#onNodeError);
this.#installNodeHandler('unhandledRejection', this.#onNodeRejection);
} else {
this.#global.addEventListener('error', this.#onBrowserError);
this.#global.addEventListener(
'unhandledrejection',
this.#onBrowserRejection
);
}
}
uninstall() {
if (
this.#global.process &&
this.#global.process.listeners &&
j$.isFunction_(this.#global.process.on)
) {
this.#nodeUninstall();
} else {
this.#browserUninstall();
}
}
// The listener at the top of the stack will be called with two arguments:
// the error and the event. Either of them may be falsy.
// The error will normally be provided, but will be falsy in the case of
// some browser load-time errors. The event will normally be provided in
// browsers but will be falsy in Node.
// Listeners that are pushed after spec files have been loaded should be
// able to just use the error parameter.
pushListener(listener) {
this.#handlers.push(listener);
}
popListener(listener) {
if (!listener) {
throw new Error('popListener expects a listener');
}
this.#handlers.pop();
}
setOverrideListener(listener, onRemove) {
if (this.#overrideHandler) {
throw new Error("Can't set more than one override listener at a time");
}
this.#overrideHandler = listener;
this.#onRemoveOverrideHandler = onRemove;
}
removeOverrideListener() {
if (this.#onRemoveOverrideHandler) {
this.#onRemoveOverrideHandler();
}
this.#overrideHandler = null;
this.#onRemoveOverrideHandler = null;
}
#nodeUninstall() {
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];
}
}
#browserUninstall() {
this.#global.removeEventListener('error', this.#onBrowserError);
this.#global.removeEventListener(
'unhandledrejection',
this.#onBrowserRejection
);
}
#dispatchBrowserError(error, event) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
overrideHandler(error);
this.#overrideHandler(error);
return;
}
const handler = handlers[handlers.length - 1];
const handler = this.#handlers[this.#handlers.length - 1];
if (handler) {
handler(error, event);
@@ -26,10 +145,30 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
}
}
this.originalHandlers = {};
this.jasmineHandlers = {};
this.installOne_ = function installOne_(errorType, jasmineMessage) {
function taggedOnError(error) {
#browserRejectionHandler(event) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
this.#dispatchBrowserError(event.reason, event);
} else {
this.#dispatchBrowserError(
'Unhandled promise rejection: ' + event.reason,
event
);
}
}
#installNodeHandler(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);
}
#handleNodeEvent(error, errorType, jasmineMessage) {
if (j$.isError_(error)) {
error.jasmineMessage = jasmineMessage + ': ' + error;
} else {
@@ -53,11 +192,11 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
error = new Error(substituteMsg);
}
const handler = handlers[handlers.length - 1];
const handler = this.#handlers[this.#handlers.length - 1];
if (overrideHandler) {
if (this.#overrideHandler) {
// See discussion of spyOnGlobalErrorsAsync in base.js
overrideHandler(error);
this.#overrideHandler(error);
return;
}
@@ -67,104 +206,6 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
throw error;
}
}
this.originalHandlers[errorType] = global.process.listeners(errorType);
this.jasmineHandlers[errorType] = taggedOnError;
global.process.removeAllListeners(errorType);
global.process.on(errorType, taggedOnError);
this.uninstall = function uninstall() {
const errorTypes = Object.keys(this.originalHandlers);
for (const errorType of errorTypes) {
global.process.removeListener(
errorType,
this.jasmineHandlers[errorType]
);
for (let i = 0; i < this.originalHandlers[errorType].length; i++) {
global.process.on(errorType, this.originalHandlers[errorType][i]);
}
delete this.originalHandlers[errorType];
delete this.jasmineHandlers[errorType];
}
};
};
this.install = function install() {
if (
global.process &&
global.process.listeners &&
j$.isFunction_(global.process.on)
) {
this.installOne_('uncaughtException', 'Uncaught exception');
this.installOne_('unhandledRejection', 'Unhandled promise rejection');
} else {
global.addEventListener('error', onBrowserError);
const browserRejectionHandler = function browserRejectionHandler(
event
) {
if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
dispatchBrowserError(event.reason, event);
} else {
dispatchBrowserError(
'Unhandled promise rejection: ' + event.reason,
event
);
}
};
global.addEventListener('unhandledrejection', browserRejectionHandler);
this.uninstall = function uninstall() {
global.removeEventListener('error', onBrowserError);
global.removeEventListener(
'unhandledrejection',
browserRejectionHandler
);
};
}
};
// The listener at the top of the stack will be called with two arguments:
// the error and the event. Either of them may be falsy.
// The error will normally be provided, but will be falsy in the case of
// some browser load-time errors. The event will normally be provided in
// browsers but will be falsy in Node.
// Listeners that are pushed after spec files have been loaded should be
// able to just use the error parameter.
this.pushListener = function pushListener(listener) {
handlers.push(listener);
};
this.popListener = function popListener(listener) {
if (!listener) {
throw new Error('popListener expects a listener');
}
handlers.pop();
};
this.setOverrideListener = function(listener, onRemove) {
if (overrideHandler) {
throw new Error("Can't set more than one override listener at a time");
}
overrideHandler = listener;
onRemoveOverrideHandler = onRemove;
};
this.removeOverrideListener = function() {
if (onRemoveOverrideHandler) {
onRemoveOverrideHandler();
}
overrideHandler = null;
onRemoveOverrideHandler = null;
};
}
return GlobalErrors;