Include stack traces in unhandled promise rejection messages

This commit is contained in:
Steve Gravrock
2020-01-20 10:50:42 -08:00
parent 7f392d188e
commit 58c63e98bb
3 changed files with 74 additions and 24 deletions

View File

@@ -3855,7 +3855,12 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
global.onerror = onerror; global.onerror = onerror;
var browserRejectionHandler = function browserRejectionHandler(event) { var browserRejectionHandler = function browserRejectionHandler(event) {
onerror('Unhandled Rejection: ' + event.reason); if (j$.isError_(event.reason)) {
event.reason.jasmineMessage = 'Unhandled promise rejection: ' + event.reason;
onerror(event.reason);
} else {
onerror('Unhandled promise rejection: ' + event.reason);
}
}; };
if (global.addEventListener) { if (global.addEventListener) {

View File

@@ -194,33 +194,72 @@ describe('GlobalErrors', function() {
); );
}); });
it('reports unhandledRejection in browser', function() { describe('Reporting unhandled promise rejections in the browser', function() {
var fakeGlobal = { it('subscribes and unsubscribes from the unhandledrejection event', function() {
addEventListener: jasmine.createSpy('addEventListener'), var fakeGlobal = jasmine.createSpyObj('globalErrors', [
removeEventListener: jasmine.createSpy('removeEventListener'), 'addEventListener',
onerror: jasmine.createSpy('onerror') 'removeEventListener',
}, 'onerror'
handler = jasmine.createSpy('errorHandler'), ]),
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal); errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install(); errors.install();
expect(fakeGlobal.addEventListener).toHaveBeenCalledWith( expect(fakeGlobal.addEventListener).toHaveBeenCalledWith(
'unhandledrejection', 'unhandledrejection',
jasmine.any(Function) jasmine.any(Function)
); );
errors.pushListener(handler); var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1];
errors.uninstall();
var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1]; expect(fakeGlobal.removeEventListener).toHaveBeenCalledWith(
addedListener({ reason: new Error('bar') }); 'unhandledrejection',
addedListener
);
});
expect(handler).toHaveBeenCalledWith('Unhandled Rejection: Error: bar'); it('reports rejections whose reason is a string', function() {
var fakeGlobal = jasmine.createSpyObj('globalErrors', [
'addEventListener',
'removeEventListener',
'onerror'
]),
handler = jasmine.createSpy('errorHandler'),
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.uninstall(); errors.install();
errors.pushListener(handler);
expect(fakeGlobal.removeEventListener).toHaveBeenCalledWith( var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1];
'unhandledrejection', addedListener({ reason: 'nope' });
addedListener
); expect(handler).toHaveBeenCalledWith('Unhandled promise rejection: nope');
});
it('reports rejections whose reason is an Error', function() {
var fakeGlobal = jasmine.createSpyObj('globalErrors', [
'addEventListener',
'removeEventListener',
'onerror'
]),
handler = jasmine.createSpy('errorHandler'),
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
errors.install();
errors.pushListener(handler);
var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1];
var reason = new Error('bar');
addedListener({ reason: reason });
var expectedError = Object.create(reason);
expectedError.jasmineMessage = expect(handler).toHaveBeenCalledWith(
jasmine.objectContaining({
jasmineMessage: 'Unhandled promise rejection: Error: bar',
message: reason.message,
stack: reason.stack
})
);
});
}); });
}); });

View File

@@ -64,7 +64,13 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
global.onerror = onerror; global.onerror = onerror;
var browserRejectionHandler = function browserRejectionHandler(event) { var browserRejectionHandler = function browserRejectionHandler(event) {
onerror('Unhandled Rejection: ' + event.reason); if (j$.isError_(event.reason)) {
event.reason.jasmineMessage =
'Unhandled promise rejection: ' + event.reason;
onerror(event.reason);
} else {
onerror('Unhandled promise rejection: ' + event.reason);
}
}; };
if (global.addEventListener) { if (global.addEventListener) {