Skip to content

Commit a150609

Browse files
committed
Display only createElement warnings
1 parent cf897f0 commit a150609

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

packages/react-error-overlay/src/overlay.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import {
2323
permanentRegister as permanentRegisterConsole,
2424
} from './effects/proxyConsole';
25+
import { massage as massageWarning } from './utils/warnings';
2526

2627
import {
2728
consume as consumeError,
@@ -210,23 +211,17 @@ function inject() {
210211
registerStackTraceLimit();
211212

212213
permanentRegisterConsole('error', warning => {
213-
const nIndex = warning.indexOf('\n');
214-
let message = warning;
215-
if (nIndex !== -1) {
216-
message = message.substring(0, nIndex);
217-
}
218-
const stack = warning.substring(nIndex + 1);
219-
window.requestAnimationFrame(function() {
220-
return crash(
221-
// $FlowFixMe
222-
{
223-
message: message,
224-
stack: stack,
225-
__unmap_source: '/static/js/bundle.js',
226-
},
227-
false
228-
);
229-
});
214+
const data = massageWarning(warning);
215+
if (data == null) return;
216+
crash(
217+
// $FlowFixMe
218+
{
219+
message: data.message,
220+
stack: data.stack,
221+
__unmap_source: '/static/js/bundle.js',
222+
},
223+
false
224+
);
230225
});
231226
}
232227

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @flow
2+
3+
// This is a list of React warnings to display
4+
// There must be zero or one capture group; and the capture group is assumed to be a missing stack frame.
5+
const warnings = [
6+
/^.*React.createElement: type is invalid.+Check your code at (.*?:.*)[.]$/,
7+
];
8+
// This is a list of information to remove from React warnings, it's not particularly useful to show
9+
const removals = [/Check your code at (.*?:.*)[.]/];
10+
11+
function massage(warning: string): { message: string, stack: string } | null {
12+
const nIndex = warning.indexOf('\n');
13+
let message = warning;
14+
if (nIndex !== -1) {
15+
message = message.substring(0, nIndex);
16+
}
17+
let stack = warning.substring(nIndex + 1);
18+
19+
let found = false;
20+
for (const warning of warnings) {
21+
const m = message.match(warning);
22+
if (!m) {
23+
continue;
24+
}
25+
found = true;
26+
if (!m[1]) {
27+
break;
28+
}
29+
stack = `in render (at ${m[1]})\n${stack}`;
30+
break;
31+
}
32+
if (!found) {
33+
return null;
34+
}
35+
36+
for (const trim of removals) {
37+
message = message.replace(trim, '');
38+
}
39+
40+
return { message, stack };
41+
}
42+
43+
export { massage };

0 commit comments

Comments
 (0)