I got this working when executed directly via jasmine when the jasmine ./tests/alLErrorException.spec.js
command is run. The following changes were required:
Always setup the listeners, even when the _callback
should not be executed.
constructor(callback, startListening = true) {
if (!callback) {
throw new Error("Missing callback function");
}
this._callback = callback;
this._listening = startListening;
this._setupEvents();
}
Add a function to intercept uncaughtException
events and to call the _callback
if we are _listening
:
_handler() {
if(this._listening) {
this._callback.apply(null, arguments);
}
}
Remove all other uncaughtException
event handlers in _setupEvents
:
_setupEvents(attatch = true) {
this._listening = attatch ? true : false;
if (attatch) {
if (typeof window !== "undefined") {
window.addEventListener("error", this._callback);
} else {
// Added this line
process.removeAllListeners('uncaughtException');
process.addListener('uncaughtException', this._handler.bind(this));
}
} else {
if (typeof window !== "undefined") {
window.removeEventListener("error", this._callback);
} else {
process.removeListener('uncaughtException', this._callback);
}
}
}
This is required because jasmine sets up it's own uncaughtException
handler and reports an error even though the error was caught by the AllErrorHandler
class.
Here is a paste of the full source for the AllErrorHandler class with the required changes.