Skip to content

Commit

Permalink
Log captured errors sooner (#10373)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bvaughn authored Aug 3, 2017
1 parent 80577eb commit 6188832
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,25 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
capturedErrors = new Map();
}

capturedErrors.set(boundary, {
const capturedError = {
componentName,
componentStack,
error,
errorBoundary: errorBoundaryFound ? boundary.stateNode : null,
errorBoundaryFound,
errorBoundaryName,
willRetry,
});
};

capturedErrors.set(boundary, capturedError);

try {
logCapturedError(capturedError);
} catch (e) {
// Prevent cycle if logCapturedError() throws.
// A cycle may still occur if logCapturedError renders a component that throws.
console.error(e);
}

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

const error = capturedError.error;
try {
logCapturedError(capturedError);
} catch (e) {
// Prevent cycle if logCapturedError() throws.
// A cycle may still occur if logCapturedError renders a component that throws.
console.error(e);
}

switch (effectfulFiber.tag) {
case ClassComponent:
const instance = effectfulFiber.stateNode;
Expand All @@ -1280,14 +1281,14 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(

// Allow the boundary to handle the error, usually by scheduling
// an update to itself
instance.componentDidCatch(error, info);
instance.componentDidCatch(capturedError.error, info);
return;
case HostRoot:
if (firstUncaughtError === null) {
// If this is the host container, we treat it as a no-op error
// boundary. We'll throw the first uncaught error once it's safe to
// do so, at the end of the batch.
firstUncaughtError = error;
firstUncaughtError = capturedError.error;
}
return;
default:
Expand Down

0 comments on commit 6188832

Please sign in to comment.