Skip to content

Commit 46d5afc

Browse files
authored
Replace console.error() with a throw in setTimeout() as last resort exception logging (#13310)
* Add a regression test for #13188 * Replace console.error() with a throw in setTimeout() as last resort * Fix lint and comment * Fix tests to check we throw after all * Fix build tests
1 parent b3b80a4 commit 46d5afc

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
* @jest-environment node
9+
*/
10+
11+
// This is a regression test for https://github.com/facebook/react/issues/13188.
12+
// It reproduces a combination of conditions that led to a problem.
13+
14+
if (global.window) {
15+
throw new Error('This test must run in a Node environment.');
16+
}
17+
18+
// The issue only reproduced when React was loaded before JSDOM.
19+
const React = require('react');
20+
const ReactDOM = require('react-dom');
21+
22+
// Unlike other tests, we want to enable error logging.
23+
// Note this is not a real Error prototype property,
24+
// it's only set in our Jest environment.
25+
// eslint-disable-next-line no-extend-native
26+
Error.prototype.suppressReactErrorLogging = false;
27+
28+
// Initialize JSDOM separately.
29+
// We don't use our normal JSDOM setup because we want to load React first.
30+
const {JSDOM} = require('jsdom');
31+
global.requestAnimationFrame = setTimeout;
32+
global.cancelAnimationFrame = clearTimeout;
33+
const jsdom = new JSDOM(`<div id="app-root"></div>`);
34+
global.window = jsdom.window;
35+
global.document = jsdom.window.document;
36+
global.navigator = jsdom.window.navigator;
37+
38+
class Bad extends React.Component {
39+
componentDidUpdate() {
40+
throw new Error('no');
41+
}
42+
render() {
43+
return null;
44+
}
45+
}
46+
47+
describe('ReactErrorLoggingRecovery', () => {
48+
let originalConsoleError = console.error;
49+
50+
beforeEach(() => {
51+
console.error = error => {
52+
throw new Error('Buggy console.error');
53+
};
54+
});
55+
56+
afterEach(() => {
57+
console.error = originalConsoleError;
58+
});
59+
60+
it('should recover from errors in console.error', function() {
61+
const div = document.createElement('div');
62+
let didCatch = false;
63+
try {
64+
ReactDOM.render(<Bad />, div);
65+
ReactDOM.render(<Bad />, div);
66+
} catch (e) {
67+
expect(e.message).toBe('no');
68+
didCatch = true;
69+
}
70+
expect(didCatch).toBe(true);
71+
ReactDOM.render(<span>Hello</span>, div);
72+
expect(div.firstChild.textContent).toBe('Hello');
73+
74+
// Verify the console.error bug is surfaced
75+
expect(() => {
76+
jest.runAllTimers();
77+
}).toThrow('Buggy console.error');
78+
});
79+
});

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ export function logError(boundary: Fiber, errorInfo: CapturedValue<mixed>) {
116116
// A cycle may still occur if logCapturedError renders a component that throws.
117117
const suppressLogging = e && e.suppressReactErrorLogging;
118118
if (!suppressLogging) {
119-
console.error(e);
119+
// Rethrow it from a clean stack because this function is assumed to never throw.
120+
// We can't safely call console.error() here because it could *also* throw if overriden.
121+
// https://github.com/facebook/react/issues/13188
122+
setTimeout(() => {
123+
throw e;
124+
});
120125
}
121126
}
122127
}

packages/react-reconciler/src/__tests__/ReactIncrementalErrorLogging-test.internal.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,10 @@ describe('ReactIncrementalErrorLogging', () => {
144144

145145
expect(logCapturedErrorCalls.length).toBe(1);
146146

147-
// The error thrown in logCapturedError should also be logged
148-
expect(console.error).toHaveBeenCalledTimes(1);
149-
expect(console.error.calls.argsFor(0)[0].message).toContain(
150-
'logCapturedError error',
151-
);
147+
// The error thrown in logCapturedError should be rethrown with a clean stack
148+
expect(() => {
149+
jest.runAllTimers();
150+
}).toThrow('logCapturedError error');
152151
} finally {
153152
jest.unmock('../ReactFiberErrorLogger');
154153
}

scripts/jest/config.build.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const packages = readdirSync(packagesRoot).filter(dir => {
1010
if (dir.charAt(0) === '.') {
1111
return false;
1212
}
13+
if (dir === 'events') {
14+
// There's an actual Node package called "events"
15+
// that's used by jsdom so we don't want to alias that.
16+
return false;
17+
}
1318
const packagePath = join(packagesRoot, dir, 'package.json');
1419
return statSync(packagePath).isFile();
1520
});

0 commit comments

Comments
 (0)