Skip to content

Commit

Permalink
Catch errors on top window using uncaughtErrorFilter
Browse files Browse the repository at this point in the history
Works around safari quirk when running in iframe
  • Loading branch information
kevinpschaaf committed Jul 18, 2018
1 parent e306692 commit 47ade19
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions test/unit/strict-template-policy.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
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
Expand All @@ -36,7 +27,9 @@
try {
callback();
} catch(error) {
window.globalError = error;
if (!window.uncaughtErrorFilter || !window.uncaughtErrorFilter(error)) {
throw error;
}
}
});
};
Expand Down Expand Up @@ -72,26 +65,34 @@
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() {
Expand Down

0 comments on commit 47ade19

Please sign in to comment.