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

Commit

Permalink
Rethrow erors in events
Browse files Browse the repository at this point in the history
Instead of manually calling window.onerror, rethrow the first error
at the end of the dispatch. This way window.onerror gets all the
expected arguments which is important for error reporters.

Fixes #408
  • Loading branch information
arv committed Apr 1, 2014
1 parent d7629dd commit 6e88e80
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 6e88e80

Please sign in to comment.