Skip to content

Commit 6188832

Browse files
authored
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.
1 parent 80577eb commit 6188832

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,15 +1189,25 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
11891189
capturedErrors = new Map();
11901190
}
11911191

1192-
capturedErrors.set(boundary, {
1192+
const capturedError = {
11931193
componentName,
11941194
componentStack,
11951195
error,
11961196
errorBoundary: errorBoundaryFound ? boundary.stateNode : null,
11971197
errorBoundaryFound,
11981198
errorBoundaryName,
11991199
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+
}
12011211

12021212
// If we're in the commit phase, defer scheduling an update on the
12031213
// boundary until after the commit is complete
@@ -1261,15 +1271,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
12611271
'bug in React. Please file an issue.',
12621272
);
12631273

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-
12731274
switch (effectfulFiber.tag) {
12741275
case ClassComponent:
12751276
const instance = effectfulFiber.stateNode;
@@ -1280,14 +1281,14 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
12801281

12811282
// Allow the boundary to handle the error, usually by scheduling
12821283
// an update to itself
1283-
instance.componentDidCatch(error, info);
1284+
instance.componentDidCatch(capturedError.error, info);
12841285
return;
12851286
case HostRoot:
12861287
if (firstUncaughtError === null) {
12871288
// If this is the host container, we treat it as a no-op error
12881289
// boundary. We'll throw the first uncaught error once it's safe to
12891290
// do so, at the end of the batch.
1290-
firstUncaughtError = error;
1291+
firstUncaughtError = capturedError.error;
12911292
}
12921293
return;
12931294
default:

0 commit comments

Comments
 (0)