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

Commit

Permalink
Merge branch 'master' into renovate/fusion-core-0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lhorie authored Jan 29, 2018
2 parents 3a97cb9 + 47fb534 commit 00094ae
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 310 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@
"./dist/browser.es2015.es.js": "./dist/browser.es2017.es.js"
},
"dependencies": {
"fusion-cli": "^0.2.0",
"fusion-cli": "^0.3.1",
"koa-bodyparser": "4.2.0"
},
"devDependencies": {
"babel-eslint": "8.2.1",
"babel-plugin-transform-flow-strip-types": "6.22.0",
"create-universal-package": "3.2.6",
"create-universal-package": "3.3.0",
"eslint": "4.16.0",
"eslint-config-fusion": "0.2.1",
"eslint-plugin-cup": "1.0.0",
"eslint-plugin-flowtype": "2.41.0",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-flowtype": "2.42.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-prettier": "2.5.0",
"eslint-plugin-react": "7.5.1",
"eslint-plugin-react": "7.6.1",
"flow-bin": "0.64.0",
"fusion-core": "0.3.0-5",
"fusion-test-utils": "0.4.0",
"fusion-tokens": "0.0.4",
"fusion-test-utils": "0.4.2",
"fusion-tokens": "0.0.5",
"nyc": "11.4.1",
"prettier": "1.10.2",
"tape-cup": "4.7.1",
"unitest": "2.1.1"
},
"peerDependencies": {
"fusion-core": "^0.3.0-4",
"fusion-tokens": "0.0.4"
"fusion-tokens": "0.0.5"
},
"scripts": {
"clean": "rm -rf dist",
Expand All @@ -63,4 +63,4 @@
"engines": {
"node": ">= 8.9.0"
}
}
}
69 changes: 35 additions & 34 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,40 @@ export const ErrorHandlingEmitterToken = createToken(
'ErrorHandlingEmitterToken'
);

export default createPlugin({
deps: {emit: ErrorHandlingEmitterToken},
provides: ({emit}) => {
const _emit =
(typeof emit === 'function' && emit) ||
((e, src) => {
if (window.onerror) window.onerror(e.message, src, null, null, e);
});
for (const key in window) {
if (
key.match(/webkit/) == null && // stop deprecation warnings
window[key] &&
window[key].prototype &&
window[key].prototype.addEventListener
) {
const proto = window[key].prototype;
const old = proto.addEventListener;
proto.addEventListener = function(type, fn, ...rest) {
const cb = function(...args) {
try {
return fn.apply(this, args);
} catch (e) {
// get exception stack frames from our own code rather than potentially from 3rd party CDN code to get around CORS issues
_emit(e, 'async-event');
}
export default __BROWSER__ &&
createPlugin({
deps: {emit: ErrorHandlingEmitterToken},
provides: ({emit}) => {
const _emit =
(typeof emit === 'function' && emit) ||
((e, src) => {
if (window.onerror) window.onerror(e.message, src, null, null, e);
});
for (const key in window) {
if (
key.match(/webkit/) == null && // stop deprecation warnings
window[key] &&
window[key].prototype &&
window[key].prototype.addEventListener
) {
const proto = window[key].prototype;
const old = proto.addEventListener;
proto.addEventListener = function(type, fn, ...rest) {
const cb = function(...args) {
try {
return fn.apply(this, args);
} catch (e) {
// get exception stack frames from our own code rather than potentially from 3rd party CDN code to get around CORS issues
_emit(e, 'async-event');
}
};
return old.call(this, type, cb, ...rest);
};
return old.call(this, type, cb, ...rest);
};
}
}
}
window.addEventListener('unhandledrejection', e => {
e.preventDefault();
_emit(e.reason instanceof Error ? e.reason : new Error(e.reason));
});
},
});
window.addEventListener('unhandledrejection', e => {
e.preventDefault();
_emit(e.reason instanceof Error ? e.reason : new Error(e.reason));
});
},
});
81 changes: 41 additions & 40 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@ const captureTypes = {

export const ErrorHandlerToken = createToken('ErrorHandlerToken');

export default createPlugin({
deps: {onError: ErrorHandlerToken},
provides({onError}) {
assert(typeof onError === 'function', '{onError} must be a function');
const err = async e => {
await onError(e, captureTypes.server);
process.exit(1);
};
process.once('uncaughtException', err);
process.once('unhandledRejection', err);
},
middleware({onError}) {
const parseBody = bodyParser();
async function middleware(ctx, next) {
if (ctx.element) {
// Here, we use GET instead of POST to avoid the need for a CSRF token
// We also avoid using fusion-plugin-universal-event batching because
// we want to collect errors even if the vendor bundle fails to load
// (e.g. due to a network timeout)
// All errors should be funneled to this handler (e.g. errors in
// addEventListener handlers, promise rejections, react render errors, etc),
// ideally by calling `window.onerror` directly with an Error object
// (which provides more robust stack traces across browsers), or via `throw`
const script = html`
export default __NODE__ &&
createPlugin({
deps: {onError: ErrorHandlerToken},
provides({onError}) {
assert(typeof onError === 'function', '{onError} must be a function');
const err = async e => {
await onError(e, captureTypes.server);
process.exit(1);
};
process.once('uncaughtException', err);
process.once('unhandledRejection', err);
},
middleware({onError}) {
const parseBody = bodyParser();
async function middleware(ctx, next) {
if (ctx.element) {
// Here, we use GET instead of POST to avoid the need for a CSRF token
// We also avoid using fusion-plugin-universal-event batching because
// we want to collect errors even if the vendor bundle fails to load
// (e.g. due to a network timeout)
// All errors should be funneled to this handler (e.g. errors in
// addEventListener handlers, promise rejections, react render errors, etc),
// ideally by calling `window.onerror` directly with an Error object
// (which provides more robust stack traces across browsers), or via `throw`
const script = html`
<script nonce="${ctx.nonce}">
onerror = function(m,s,l,c,e) {
var _e = e || {};
Expand All @@ -58,20 +59,20 @@ onerror = function(m,s,l,c,e) {
_e.__handled = true;
};
</script>`;
ctx.template.head.unshift(script);
} else if (ctx.path === ctx.prefix + '/_errors') {
await parseBody(ctx, () => Promise.resolve());
await onError(ctx.request.body, captureTypes.browser);
ctx.body = {ok: 1};
ctx.template.head.unshift(script);
} else if (ctx.path === ctx.prefix + '/_errors') {
await parseBody(ctx, () => Promise.resolve());
await onError(ctx.request.body, captureTypes.browser);
ctx.body = {ok: 1};
}
try {
await next();
} catch (e) {
// Don't await onError here because we want to send a response as soon as possible to the user
onError(e, captureTypes.request);
throw e;
}
}
try {
await next();
} catch (e) {
// Don't await onError here because we want to send a response as soon as possible to the user
onError(e, captureTypes.request);
throw e;
}
}
return middleware;
},
});
return middleware;
},
});
Loading

0 comments on commit 00094ae

Please sign in to comment.