Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #413 from arv/error-in-events
Browse files Browse the repository at this point in the history
Rethrow erors in events
  • Loading branch information
Steve Orvell committed Apr 8, 2014
2 parents e9238fd + 6e88e80 commit a86a1b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/wrappers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,22 @@
return getTreeScope(a) === getTreeScope(b);
}

// pendingError is used to rethrow the first error we got during an event
// dispatch. The browser actually reports all errors but to do that we would
// need to rethrow the error asynchronously.
var pendingError;

function dispatchOriginalEvent(originalEvent) {
// Make sure this event is only dispatched once.
if (handledEventsTable.get(originalEvent))
return;
handledEventsTable.set(originalEvent, true);
dispatchEvent(wrap(originalEvent), wrap(originalEvent.target));
if (pendingError) {
var err = pendingError;
pendingError = null;
throw err;
}
}

function isLoadLikeEvent(event) {
Expand Down Expand Up @@ -324,10 +334,8 @@
return false;

} catch (ex) {
if (window.onerror)
window.onerror(ex.message);
else
console.error(ex, ex.stack);
if (!pendingError)
pendingError = ex;
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -1385,4 +1385,30 @@ test('retarget order (multiple shadow roots)', function() {
assert.isFalse(checkbox.checked);
});

test('window.onerror', function() {
var old = window.onerror;
var f;
var msg = 'Intentional error';
var errorCount = 0;

window.onerror = function(msg, source, lineNumber, columnNumber, error) {
document.removeEventListener('click', f);
window.onerror = old;
assert.isTrue(msg.indexOf(msg) >= 0);
assert.typeOf(source, 'string');
assert.typeOf(lineNumber, 'number');
assert.typeOf(columnNumber, 'number');
// error is not available in IE11.
errorCount++;
};

document.addEventListener('click', f = function(e) {
throw new Error(msg);
});

document.body.click();

assert.equal(errorCount, 1);
});

});

0 comments on commit a86a1b9

Please sign in to comment.