Enable custom promise error handling through overriding of global.onerror
This commit is contained in:
committed by
Steve Gravrock
parent
88de272c89
commit
905e3fc3f9
@@ -12,6 +12,23 @@ describe('GlobalErrors', function() {
|
||||
expect(handler).toHaveBeenCalledWith('foo');
|
||||
});
|
||||
|
||||
it('enables external interception of error by overriding global.onerror', function() {
|
||||
var fakeGlobal = { onerror: null },
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
hijackHandler = jasmine.createSpy('hijackErrorHandler'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler);
|
||||
|
||||
fakeGlobal.onerror = hijackHandler;
|
||||
|
||||
fakeGlobal.onerror('foo');
|
||||
|
||||
expect(hijackHandler).toHaveBeenCalledWith('foo');
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls the global error handler with all parameters', function() {
|
||||
var fakeGlobal = { onerror: null },
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
@@ -285,5 +302,64 @@ describe('GlobalErrors', function() {
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
describe('Enabling external interception of reported rejections by overriding global.onerror', function() {
|
||||
it('overriding global.onerror intercepts rejections whose reason is a string', function() {
|
||||
var fakeGlobal = jasmine.createSpyObj('globalErrors', [
|
||||
'addEventListener'
|
||||
]),
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
hijackHandler = jasmine.createSpy('hijackErrorHandler'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler);
|
||||
|
||||
fakeGlobal.onerror = hijackHandler;
|
||||
|
||||
var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1];
|
||||
addedListener({ reason: 'nope' });
|
||||
|
||||
expect(hijackHandler).toHaveBeenCalledWith(
|
||||
'Unhandled promise rejection: nope'
|
||||
);
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('overriding global.onerror intercepts rejections whose reason is an Error', function() {
|
||||
var fakeGlobal = jasmine.createSpyObj('globalErrors', [
|
||||
'addEventListener'
|
||||
]),
|
||||
handler = jasmine.createSpy('errorHandler'),
|
||||
hijackHandler = jasmine.createSpy('hijackErrorHandler'),
|
||||
errors = new jasmineUnderTest.GlobalErrors(fakeGlobal);
|
||||
|
||||
errors.install();
|
||||
errors.pushListener(handler);
|
||||
|
||||
fakeGlobal.onerror = hijackHandler;
|
||||
|
||||
var addedListener = fakeGlobal.addEventListener.calls.argsFor(0)[1];
|
||||
var reason;
|
||||
|
||||
try {
|
||||
// Throwing ensures that we get a stack property in all browsers
|
||||
throw new Error('bar');
|
||||
} catch (e) {
|
||||
reason = e;
|
||||
}
|
||||
|
||||
addedListener({ reason: reason });
|
||||
|
||||
expect(hijackHandler).toHaveBeenCalledWith(
|
||||
jasmine.objectContaining({
|
||||
jasmineMessage: 'Unhandled promise rejection: Error: bar',
|
||||
message: reason.message,
|
||||
stack: reason.stack
|
||||
})
|
||||
);
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -67,9 +67,9 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
|
||||
if (j$.isError_(event.reason)) {
|
||||
event.reason.jasmineMessage =
|
||||
'Unhandled promise rejection: ' + event.reason;
|
||||
onerror(event.reason);
|
||||
global.onerror(event.reason);
|
||||
} else {
|
||||
onerror('Unhandled promise rejection: ' + event.reason);
|
||||
global.onerror('Unhandled promise rejection: ' + event.reason);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user