Catch and report unhandled rejections
Promise rejection events are not widely supported yet, but we can rely on them in Chrome. This closes a blind spot our reporting system has when it comes to uncaught exceptions in asynchronous operations. Change-Id: I3ee2def42ee3dfb9162b5d6386bf3cf9f5e00cf9
This commit is contained in:
@@ -64,6 +64,13 @@
|
|||||||
const catchErrors = function(opt_context) {
|
const catchErrors = function(opt_context) {
|
||||||
const context = opt_context || window;
|
const context = opt_context || window;
|
||||||
context.onerror = onError.bind(null, context.onerror);
|
context.onerror = onError.bind(null, context.onerror);
|
||||||
|
context.addEventListener('unhandledrejection', e => {
|
||||||
|
const msg = e.reason.message;
|
||||||
|
const payload = {
|
||||||
|
error: e.reason,
|
||||||
|
};
|
||||||
|
GrReporting.prototype.reporter(ERROR.TYPE, ERROR.CATEGORY, msg, payload);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
catchErrors();
|
catchErrors();
|
||||||
|
|
||||||
|
|||||||
@@ -183,7 +183,12 @@ limitations under the License.
|
|||||||
|
|
||||||
setup(() => {
|
setup(() => {
|
||||||
reporter = sandbox.stub(GrReporting.prototype, 'reporter');
|
reporter = sandbox.stub(GrReporting.prototype, 'reporter');
|
||||||
fakeWindow = {};
|
fakeWindow = {
|
||||||
|
handlers: {},
|
||||||
|
addEventListener(type, handler) {
|
||||||
|
this.handlers[type] = handler;
|
||||||
|
},
|
||||||
|
};
|
||||||
sandbox.stub(console, 'error');
|
sandbox.stub(console, 'error');
|
||||||
window.GrReporting._catchErrors(fakeWindow);
|
window.GrReporting._catchErrors(fakeWindow);
|
||||||
});
|
});
|
||||||
@@ -204,6 +209,15 @@ limitations under the License.
|
|||||||
test('prevent default event handler', () => {
|
test('prevent default event handler', () => {
|
||||||
assert.isTrue(emulateThrow());
|
assert.isTrue(emulateThrow());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('unhandled rejection', () => {
|
||||||
|
fakeWindow.handlers['unhandledrejection']({
|
||||||
|
reason: {
|
||||||
|
message: 'bar',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
assert.isTrue(reporter.calledWith('error', 'exception', 'bar'));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user