Catch errors on top window using uncaughtErrorFilter

Works around safari quirk when running in iframe
This commit is contained in:
Kevin Schaaf
2018-07-09 21:31:29 -07:00
parent e3066924b7
commit 47ade191de

View File

@@ -18,15 +18,6 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
import { setStrictTemplatePolicy } from '../../lib/utils/settings.js';
import { Debouncer } from '../../lib/utils/debounce.js';
setStrictTemplatePolicy(true);
// Errors thrown in custom element reactions are not thrown up
// the call stack to the dom methods that provoked them, so need
// to catch them here and prevent mocha from complaining about them
window.addEventListener('error', function(event) {
event.stopImmediatePropagation();
if (!window.globalError) {
window.globalError = event;
}
});
// Errors thrown in Polymer's debouncer queue get re-thrown
// via setTimeout, making them particulary difficult to verify;
// Wrap debouncer callbacks and store on the globalError to test later
@@ -36,7 +27,9 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
try {
callback();
} catch(error) {
window.globalError = error;
if (!window.uncaughtErrorFilter || !window.uncaughtErrorFilter(error)) {
throw error;
}
}
});
};
@@ -72,26 +65,34 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
suite('strictTemplatePolicy', function() {
teardown(function() {
window.globalError = null;
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = null;
});
// Errors thrown in custom element reactions are not thrown up
// the call stack to the dom methods that provoked them, so this
// wraps Chai's assert.throws to re-throw uncaught errors
function assertThrows(fn, re) {
let uncaughtError = null;
// Catch uncaught errors; note when running in iframe sometimes
// Safari errors are thrown on the top window, sometimes not, so
// catch in both places
window.uncaughtErrorFilter = window.top.uncaughtErrorFilter = function(err) {
uncaughtError = err;
return true;
}
assert.throws(function() {
fn();
// Throw any errors caught on window
if (window.globalError) {
throw new Error(window.globalError.message);
// Re-throw any uncaughtErrors
if (uncaughtError) {
throw new Error(uncaughtError.message);
}
// Force polyfill reactions and/or async template stamping
flush();
// Throw any add'l errors caught on window
if (window.globalError) {
throw new Error(window.globalError.message);
// Re-throw any uncaughtErrors
if (uncaughtError) {
throw new Error(uncaughtError.message);
}
}, re);
if (HTMLTemplateElement.bootstrap) {
HTMLTemplateElement.bootstrap();
}
}
test('dom-bind', function() {