Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Make ErrorHandlingEmitterToken optional, default to calling window.onError #60

Merged
merged 3 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/__tests__/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getSimulator} from 'fusion-test-utils';
import ErrorHandling, {ErrorHandlingEmitterToken} from '../client';

test('Get exception stack frames', t => {
t.plan(2);
t.plan(4);

const app = new App('test', el => el);

Expand All @@ -21,26 +21,30 @@ test('Get exception stack frames', t => {
t.equal(e, mockError, 'emits error');
};

const mockAddEventListener = () => t.pass('called original register');
window.__foo__ = {
prototype: {
addEventListener: mockAddEventListener,
},
};
function h() {
throw mockError;
}

class Foo {
addEventListener(event, handler) {
t.equal(event, 'some-event', 'passes event type through');
t.notEqual(handler, h, 'wraps handler in try catch');
handler();
}
}

window.__foo__ = Foo;

app.register(ErrorHandling);
app.register(ErrorHandlingEmitterToken, mockEmit);
getSimulator(app);

t.notEqual(
window.__foo__.prototype.addEventListener,
mockAddEventListener,
'addEventListener wrapped'
);
getSimulator(app);

const rejectionEvent = new Event('unhandledrejection');
rejectionEvent.reason = mockError;
window.dispatchEvent(rejectionEvent);
try {
window.__foo__.prototype.addEventListener('some-event', h);
} catch (e) {
t.equal(e, mockError, 'throws error in dev');
}

t.end();
});
18 changes: 13 additions & 5 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
/* eslint-env browser */

import {createPlugin} from 'fusion-core';
import {createToken} from 'fusion-tokens';
import {createOptionalToken} from 'fusion-tokens';

export const ErrorHandlingEmitterToken = createToken(
'ErrorHandlingEmitterToken'
export const ErrorHandlingEmitterToken = createOptionalToken(
'ErrorHandlingEmitterToken',
null
);

export default __BROWSER__ &&
createPlugin({
deps: {emit: ErrorHandlingEmitterToken},
provides: ({emit}) => {
const _emit =
(typeof emit === 'function' && emit) ||
let _emit =
emit ||
((e, src) => {
if (window.onerror) window.onerror(e.message, src, null, null, e);
});
if (__DEV__) {
let oldEmit = _emit;
_emit = (e, src) => {
oldEmit(e, src);
throw e;
};
}
for (const key in window) {
if (
key.match(/webkit/) == null && // stop deprecation warnings
Expand Down