Skip to content

Commit 64e6721

Browse files
yungstersfacebook-github-bot
authored andcommitted
Do not passthrough logbox errors that already have a component stack (#44888)
Summary: Pull Request resolved: #44888 In facebook/react#29839 we removed the `Warning: ` prefix. This PR replaces the special cases in LogBox for `Warning: ` to use the presence of a component stack instead. This is what LogBox really cares about anyway, since the reason to let errors pass through to the exception manager is to let DevTools add the component stacks. Changelog: [General] [Fixed] - Fix logbox reporting for React errors Reviewed By: rickhanlonii Differential Revision: D58441017 fbshipit-source-id: 5355cd04ddcd5238dadbfcbd64fe1f43c8cd04dc
1 parent 2f8d4f0 commit 64e6721

File tree

9 files changed

+184
-49
lines changed

9 files changed

+184
-49
lines changed

packages/react-native/Libraries/LogBox/Data/LogBoxData.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {LogLevel} from './LogBoxLog';
1313
import type {
1414
Category,
1515
ComponentStack,
16+
ComponentStackType,
1617
ExtendedExceptionData,
1718
Message,
1819
} from './parseLogBoxLog';
@@ -30,6 +31,7 @@ export type LogData = $ReadOnly<{
3031
message: Message,
3132
category: Category,
3233
componentStack: ComponentStack,
34+
componentStackType: ComponentStackType | null,
3335
stack?: string,
3436
}>;
3537

@@ -215,6 +217,7 @@ export function addLog(log: LogData): void {
215217
stack,
216218
category: log.category,
217219
componentStack: log.componentStack,
220+
componentStackType: log.componentStackType || 'legacy',
218221
}),
219222
);
220223
} catch (error) {

packages/react-native/Libraries/LogBox/Data/__tests__/LogBoxData-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const addLogs = (logs: Array<string>, options: void | {flush: boolean}) => {
6565
},
6666
category: message,
6767
componentStack: [],
68+
componentStackType: null,
6869
});
6970
if (options == null || options.flush !== false) {
7071
jest.runOnlyPendingTimers();

packages/react-native/Libraries/LogBox/Data/__tests__/parseLogBoxLog-test.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('parseLogBoxLog', () => {
119119
it('does not duplicate message if component stack found but not parsed', () => {
120120
expect(
121121
parseLogBoxLog([
122-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
122+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
123123
'\n\nCheck the render method of `MyOtherComponent`.',
124124
'',
125125
'\n in\n in\n in',
@@ -128,18 +128,18 @@ describe('parseLogBoxLog', () => {
128128
componentStackType: 'legacy',
129129
componentStack: [],
130130
category:
131-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
131+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
132132
message: {
133133
content:
134-
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
134+
'Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
135135
substitutions: [
136136
{
137137
length: 48,
138-
offset: 62,
138+
offset: 53,
139139
},
140140
{
141141
length: 0,
142-
offset: 110,
142+
offset: 101,
143143
},
144144
],
145145
},
@@ -149,7 +149,7 @@ describe('parseLogBoxLog', () => {
149149
it('detects a component stack in an interpolated warning', () => {
150150
expect(
151151
parseLogBoxLog([
152-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
152+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
153153
'\n\nCheck the render method of `Container(Component)`.',
154154
'\n in MyComponent (at filename.js:1)\n in MyOtherComponent (at filename2.js:1)',
155155
]),
@@ -168,14 +168,14 @@ describe('parseLogBoxLog', () => {
168168
},
169169
],
170170
category:
171-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
171+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
172172
message: {
173173
content:
174-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `Container(Component)`.',
174+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `Container(Component)`.',
175175
substitutions: [
176176
{
177177
length: 52,
178-
offset: 129,
178+
offset: 120,
179179
},
180180
],
181181
},
@@ -805,7 +805,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
805805
it('detects a component stack in an interpolated warning', () => {
806806
expect(
807807
parseLogBoxLog([
808-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
808+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
809809
'\n\nCheck the render method of `MyComponent`.',
810810
'\n in MyComponent (created by MyOtherComponent)\n in MyOtherComponent (created by MyComponent)\n in MyAppComponent (created by MyOtherComponent)',
811811
]),
@@ -829,14 +829,14 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
829829
},
830830
],
831831
category:
832-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
832+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
833833
message: {
834834
content:
835-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
835+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
836836
substitutions: [
837837
{
838838
length: 43,
839-
offset: 129,
839+
offset: 120,
840840
},
841841
],
842842
},
@@ -911,7 +911,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
911911
it('detects a component stack in the nth argument', () => {
912912
expect(
913913
parseLogBoxLog([
914-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
914+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
915915
'\n\nCheck the render method of `MyOtherComponent`.',
916916
'',
917917
'\n in MyComponent (created by MyOtherComponent)\n in MyOtherComponent (created by MyComponent)\n in MyAppComponent (created by MyOtherComponent)',
@@ -936,18 +936,18 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
936936
},
937937
],
938938
category:
939-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
939+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
940940
message: {
941941
content:
942-
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
942+
'Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
943943
substitutions: [
944944
{
945945
length: 48,
946-
offset: 62,
946+
offset: 53,
947947
},
948948
{
949949
length: 0,
950-
offset: 110,
950+
offset: 101,
951951
},
952952
],
953953
},
@@ -1080,7 +1080,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
10801080
it('detects a component stack in an interpolated warning', () => {
10811081
expect(
10821082
parseLogBoxLog([
1083-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
1083+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
10841084
'\n\nCheck the render method of `MyComponent`.',
10851085
'\n at MyComponent (/path/to/filename.js:1:2)\n at MyOtherComponent\n at MyAppComponent (/path/to/app.js:100:20)',
10861086
]),
@@ -1103,14 +1103,14 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
11031103
},
11041104
],
11051105
category:
1106-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
1106+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
11071107
message: {
11081108
content:
1109-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
1109+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
11101110
substitutions: [
11111111
{
11121112
length: 43,
1113-
offset: 129,
1113+
offset: 120,
11141114
},
11151115
],
11161116
},
@@ -1183,7 +1183,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
11831183
it('detects a component stack in the nth argument', () => {
11841184
expect(
11851185
parseLogBoxLog([
1186-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
1186+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
11871187
'\n\nCheck the render method of `MyOtherComponent`.',
11881188
'',
11891189
'\n at MyComponent (/path/to/filename.js:1:2)\n at MyOtherComponent\n at MyAppComponent (/path/to/app.js:100:20)',
@@ -1207,18 +1207,18 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
12071207
},
12081208
],
12091209
category:
1210-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
1210+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
12111211
message: {
12121212
content:
1213-
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
1213+
'Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
12141214
substitutions: [
12151215
{
12161216
length: 48,
1217-
offset: 62,
1217+
offset: 53,
12181218
},
12191219
{
12201220
length: 0,
1221-
offset: 110,
1221+
offset: 101,
12221222
},
12231223
],
12241224
},
@@ -1289,7 +1289,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
12891289
it('detects a component stack in an interpolated warning', () => {
12901290
expect(
12911291
parseLogBoxLog([
1292-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
1292+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s%s',
12931293
'\n\nCheck the render method of `MyComponent`.',
12941294
'\nMyComponent@/path/to/filename.js:1:2\nforEach@[native code]\nMyAppComponent@/path/to/app.js:100:20',
12951295
]),
@@ -1316,14 +1316,14 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
13161316
},
13171317
],
13181318
category:
1319-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
1319+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?%s',
13201320
message: {
13211321
content:
1322-
'Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
1322+
'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?\n\nCheck the render method of `MyComponent`.',
13231323
substitutions: [
13241324
{
13251325
length: 43,
1326-
offset: 129,
1326+
offset: 120,
13271327
},
13281328
],
13291329
},
@@ -1488,7 +1488,7 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
14881488
it('detects a component stack in the nth argument', () => {
14891489
expect(
14901490
parseLogBoxLog([
1491-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
1491+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.%s',
14921492
'\n\nCheck the render method of `MyOtherComponent`.',
14931493
'',
14941494
'\nMyComponent@/path/to/filename.js:1:2\nforEach@[native code]\nMyAppComponent@/path/to/app.js:100:20',
@@ -1516,18 +1516,18 @@ Please follow the instructions at: fburl.com/rn-remote-assets`,
15161516
},
15171517
],
15181518
category:
1519-
'Warning: Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
1519+
'Each child in a list should have a unique "key" prop.%s%s See https://fb.me/react-warning-keys for more information.',
15201520
message: {
15211521
content:
1522-
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
1522+
'Each child in a list should have a unique "key" prop.\n\nCheck the render method of `MyOtherComponent`. See https://fb.me/react-warning-keys for more information.',
15231523
substitutions: [
15241524
{
15251525
length: 48,
1526-
offset: 62,
1526+
offset: 53,
15271527
},
15281528
{
15291529
length: 0,
1530-
offset: 110,
1530+
offset: 101,
15311531
},
15321532
],
15331533
},

packages/react-native/Libraries/LogBox/Data/parseLogBoxLog.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ const RE_BABEL_CODE_FRAME_MARKER_PATTERN = new RegExp(
9393
'm',
9494
);
9595

96+
export function hasComponentStack(args: $ReadOnlyArray<mixed>): boolean {
97+
for (const arg of args) {
98+
if (typeof arg === 'string' && isComponentStack(arg)) {
99+
return true;
100+
}
101+
}
102+
return false;
103+
}
104+
96105
export type ExtendedExceptionData = ExceptionData & {
97106
isComponentError: boolean,
98107
...

packages/react-native/Libraries/LogBox/LogBox.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {ExtendedExceptionData} from './Data/parseLogBoxLog';
1313

1414
import Platform from '../Utilities/Platform';
1515
import RCTLog from '../Utilities/RCTLog';
16+
import {hasComponentStack} from './Data/parseLogBoxLog';
1617

1718
export type {LogData, ExtendedExceptionData, IgnorePattern};
1819

@@ -150,14 +151,16 @@ if (__DEV__) {
150151

151152
try {
152153
if (!isRCTLogAdviceWarning(...args)) {
153-
const {category, message, componentStack} = parseLogBoxLog(args);
154+
const {category, message, componentStack, componentStackType} =
155+
parseLogBoxLog(args);
154156

155157
if (!LogBoxData.isMessageIgnored(message.content)) {
156158
LogBoxData.addLog({
157159
level: 'warn',
158160
category,
159161
message,
160162
componentStack,
163+
componentStackType,
161164
});
162165
}
163166
}
@@ -176,12 +179,20 @@ if (__DEV__) {
176179
}
177180

178181
try {
179-
if (!isWarningModuleWarning(...args)) {
180-
// Only show LogBox for the 'warning' module, otherwise pass through.
182+
if (!isWarningModuleWarning(...args) && !hasComponentStack(args)) {
183+
// Only show LogBox for the 'warning' module, or React errors with
184+
// component stacks, otherwise pass the error through.u
185+
//
181186
// By passing through, this will get picked up by the React console override,
182187
// potentially adding the component stack. React then passes it back to the
183188
// React Native ExceptionsManager, which reports it to LogBox as an error.
184189
//
190+
// Ideally, we refactor all RN error handling so that LogBox patching
191+
// errors is not necessary, and they are reported the same as a framework.
192+
// The blocker to this is that the ExceptionManager console.error override
193+
// strigifys all of the args before passing it through to LogBox, which
194+
// would lose all of the interpolation information.
195+
//
185196
// The 'warning' module needs to be handled here because React internally calls
186197
// `console.error('Warning: ')` with the component stack already included.
187198
originalConsoleError(...args);
@@ -190,20 +201,25 @@ if (__DEV__) {
190201

191202
const format = args[0].replace('Warning: ', '');
192203
const filterResult = LogBoxData.checkWarningFilter(format);
193-
if (filterResult.suppressCompletely) {
194-
return;
195-
}
196-
197204
let level = 'error';
198-
if (filterResult.suppressDialog_LEGACY === true) {
199-
level = 'warn';
200-
} else if (filterResult.forceDialogImmediately === true) {
201-
level = 'fatal'; // Do not downgrade. These are real bugs with same severity as throws.
205+
if (filterResult.monitorEvent !== 'warning_unhandled') {
206+
if (filterResult.suppressCompletely) {
207+
return;
208+
}
209+
210+
if (filterResult.suppressDialog_LEGACY === true) {
211+
level = 'warn';
212+
} else if (filterResult.forceDialogImmediately === true) {
213+
level = 'fatal'; // Do not downgrade. These are real bugs with same severity as throws.
214+
}
202215
}
203216

204217
// Unfortunately, we need to add the Warning: prefix back for downstream dependencies.
218+
// Downstream, we check for this prefix to know that LogBox already handled it, so
219+
// it doesn't get reported back to LogBox. It's an absolute mess.
205220
args[0] = `Warning: ${filterResult.finalFormat}`;
206-
const {category, message, componentStack} = parseLogBoxLog(args);
221+
const {category, message, componentStack, componentStackType} =
222+
parseLogBoxLog(args);
207223

208224
// Interpolate the message so they are formatted for adb and other CLIs.
209225
// This is different than the message.content above because it includes component stacks.
@@ -216,6 +232,7 @@ if (__DEV__) {
216232
category,
217233
message,
218234
componentStack,
235+
componentStackType,
219236
});
220237
}
221238
} catch (err) {

packages/react-native/Libraries/LogBox/__tests__/LogBox-integration-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ describe.skip('LogBox', () => {
9595
expect(mockError.mock.calls[0].map(cleanPath)).toMatchSnapshot(
9696
'Log passed to console error',
9797
);
98+
99+
// The Warning: prefix is added due to a hack in LogBox to prevent double logging.
98100
expect(mockError.mock.calls[0][0].startsWith('Warning: ')).toBe(true);
99101
});
100102

@@ -123,6 +125,8 @@ describe.skip('LogBox', () => {
123125
expect(mockError.mock.calls[0].map(cleanPath)).toMatchSnapshot(
124126
'Log passed to console error',
125127
);
128+
129+
// The Warning: prefix is added due to a hack in LogBox to prevent double logging.
126130
expect(mockError.mock.calls[0][0].startsWith('Warning: ')).toBe(true);
127131
});
128132
});

0 commit comments

Comments
 (0)