@samitouri / QOS-React / commits / 6188832c5e

Log captured errors sooner (#10373)

This prevents the captured error from becoming separated from the component stack if other errors (or console.error calls) are made between them, eg a component errors during unmount.

Brian Vaughn committed Aug 3, 2017 at 15:51 UTC 6188832c5e9113937bae65fe2c189b39608cf80a
1 file changed +14 -13
src/renderers/shared/fiber/ReactFiberScheduler.js
+14 -13
@@ -1189,7 +1189,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
1189 capturedErrors = new Map();
1190 }
1191
1192 - capturedErrors.set(boundary, {
1192 + const capturedError = {
1193 componentName,
1194 componentStack,
1195 error,
@@ -1197,7 +1197,17 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
1197 errorBoundaryFound,
1198 errorBoundaryName,
1199 willRetry,
1200 - });
1200 + };
1201 +
1202 + capturedErrors.set(boundary, capturedError);
1203 +
1204 + try {
1205 + logCapturedError(capturedError);
1206 + } catch (e) {
1207 + // Prevent cycle if logCapturedError() throws.
1208 + // A cycle may still occur if logCapturedError renders a component that throws.
1209 + console.error(e);
1210 + }
1211
1212 // If we're in the commit phase, defer scheduling an update on the
1213 // boundary until after the commit is complete
@@ -1261,15 +1271,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
1271 'bug in React. Please file an issue.',
1272 );
1273
1264 - const error = capturedError.error;
1265 - try {
1266 - logCapturedError(capturedError);
1267 - } catch (e) {
1268 - // Prevent cycle if logCapturedError() throws.
1269 - // A cycle may still occur if logCapturedError renders a component that throws.
1270 - console.error(e);
1271 - }
1272 -
1274 switch (effectfulFiber.tag) {
1275 case ClassComponent:
1276 const instance = effectfulFiber.stateNode;
@@ -1280,14 +1281,14 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
1281
1282 // Allow the boundary to handle the error, usually by scheduling
1283 // an update to itself
1283 - instance.componentDidCatch(error, info);
1284 + instance.componentDidCatch(capturedError.error, info);
1285 return;
1286 case HostRoot:
1287 if (firstUncaughtError === null) {
1288 // If this is the host container, we treat it as a no-op error
1289 // boundary. We'll throw the first uncaught error once it's safe to
1290 // do so, at the end of the batch.
1290 - firstUncaughtError = error;
1291 + firstUncaughtError = capturedError.error;
1292 }
1293 return;
1294 default: