Files
jasmine/src/core/GlobalErrors.js
Steve Gravrock 10f1220e55 Don't mask errors that occur when no handlers are installed
It's possible for async code to cause an error when Jasmine
doesn't have any listeners registered internally. This causes
Jasmine to crash (Node) or log to the console (browser)
because of trying to call the nonexistent handler. This change
doesn't fix the overall problem but it does ensure that the
original error is logged rather than Jasmine's internal error.
2017-05-08 11:09:32 -07:00

51 lines
1.4 KiB
JavaScript

getJasmineRequireObj().GlobalErrors = function(j$) {
function GlobalErrors(global) {
var handlers = [];
global = global || j$.getGlobal();
var onerror = function onerror() {
var handler = handlers[handlers.length - 1];
if (handler) {
handler.apply(null, Array.prototype.slice.call(arguments, 0));
} else {
throw arguments[0];
}
};
this.uninstall = function noop() {};
this.install = function install() {
if (global.process && global.process.listeners && j$.isFunction_(global.process.on)) {
var originalHandlers = global.process.listeners('uncaughtException');
global.process.removeAllListeners('uncaughtException');
global.process.on('uncaughtException', onerror);
this.uninstall = function uninstall() {
global.process.removeListener('uncaughtException', onerror);
for (var i = 0; i < originalHandlers.length; i++) {
global.process.on('uncaughtException', originalHandlers[i]);
}
};
} else {
var originalHandler = global.onerror;
global.onerror = onerror;
this.uninstall = function uninstall() {
global.onerror = originalHandler;
};
}
};
this.pushListener = function pushListener(listener) {
handlers.push(listener);
};
this.popListener = function popListener() {
handlers.pop();
};
}
return GlobalErrors;
};