Skip to content

Commit

Permalink
Display only createElement warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed May 11, 2017
1 parent cf897f0 commit a150609
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
29 changes: 12 additions & 17 deletions packages/react-error-overlay/src/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import {
permanentRegister as permanentRegisterConsole,
} from './effects/proxyConsole';
import { massage as massageWarning } from './utils/warnings';

import {
consume as consumeError,
Expand Down Expand Up @@ -210,23 +211,17 @@ function inject() {
registerStackTraceLimit();

permanentRegisterConsole('error', warning => {
const nIndex = warning.indexOf('\n');
let message = warning;
if (nIndex !== -1) {
message = message.substring(0, nIndex);
}
const stack = warning.substring(nIndex + 1);
window.requestAnimationFrame(function() {
return crash(
// $FlowFixMe
{
message: message,
stack: stack,
__unmap_source: '/static/js/bundle.js',
},
false
);
});
const data = massageWarning(warning);
if (data == null) return;
crash(
// $FlowFixMe
{
message: data.message,
stack: data.stack,
__unmap_source: '/static/js/bundle.js',
},
false
);
});
}

Expand Down
43 changes: 43 additions & 0 deletions packages/react-error-overlay/src/utils/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @flow

// This is a list of React warnings to display
// There must be zero or one capture group; and the capture group is assumed to be a missing stack frame.
const warnings = [
/^.*React.createElement: type is invalid.+Check your code at (.*?:.*)[.]$/,
];
// This is a list of information to remove from React warnings, it's not particularly useful to show
const removals = [/Check your code at (.*?:.*)[.]/];

function massage(warning: string): { message: string, stack: string } | null {
const nIndex = warning.indexOf('\n');
let message = warning;
if (nIndex !== -1) {
message = message.substring(0, nIndex);
}
let stack = warning.substring(nIndex + 1);

let found = false;
for (const warning of warnings) {
const m = message.match(warning);
if (!m) {
continue;
}
found = true;
if (!m[1]) {
break;
}
stack = `in render (at ${m[1]})\n${stack}`;
break;
}
if (!found) {
return null;
}

for (const trim of removals) {
message = message.replace(trim, '');
}

return { message, stack };
}

export { massage };

0 comments on commit a150609

Please sign in to comment.