Skip to content

Commit

Permalink
Move error logging to update callback
Browse files Browse the repository at this point in the history
This prevents double logging for gDSFE boundaries with createRoot.
  • Loading branch information
gaearon committed Jun 23, 2021
1 parent 8dc9c27 commit d1a3291
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,6 @@ describe('ReactDOMConsoleErrorReporting', () => {
message: 'Boom',
}),
],
[
// Addendum by React:
expect.stringContaining(
'The above error occurred in the <Foo> component',
),
],
[
// TODO: This is duplicated only with createRoot. Why?
expect.stringContaining('Error: Uncaught [Error: Boom]'),
Expand All @@ -274,7 +268,7 @@ describe('ReactDOMConsoleErrorReporting', () => {
}),
],
[
// TODO: This is duplicated only with createRoot. Why?
// Addendum by React:
expect.stringContaining(
'The above error occurred in the <Foo> component',
),
Expand All @@ -291,12 +285,6 @@ describe('ReactDOMConsoleErrorReporting', () => {
message: 'Boom',
}),
],
[
// TODO: This is duplicated only with createRoot. Why?
expect.objectContaining({
message: 'Boom',
}),
],
]);
}

Expand Down
15 changes: 7 additions & 8 deletions packages/react-reconciler/src/ReactFiberThrow.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ function createClassErrorUpdate(
if (typeof getDerivedStateFromError === 'function') {
const error = errorInfo.value;
update.payload = () => {
logCapturedError(fiber, errorInfo);
return getDerivedStateFromError(error);
};
update.callback = () => {

This comment has been minimized.

Copy link
@gaearon

gaearon Jun 23, 2021

Author Collaborator

Moving the block from below. It used to be DEV-only for gDSFE but now there's a line that has PROD too.

if (__DEV__) {
markFailedErrorBoundaryForHotReloading(fiber);
}
logCapturedError(fiber, errorInfo);
};
}

const inst = fiber.stateNode;
Expand All @@ -119,16 +124,14 @@ function createClassErrorUpdate(
if (__DEV__) {
markFailedErrorBoundaryForHotReloading(fiber);
}
logCapturedError(fiber, errorInfo);

This comment has been minimized.

Copy link
@gaearon

gaearon Jun 23, 2021

Author Collaborator

Doesn't need to be conditional anymore. The condition was needed because we used to have logging both in payload and callback. Now it's always in callback, and the previous callback is being overridden here so we don't need to worry about the previously assigned implementation.

if (typeof getDerivedStateFromError !== 'function') {
// To preserve the preexisting retry behavior of error boundaries,
// we keep track of which ones already failed during this batch.
// This gets reset before we yield back to the browser.
// TODO: Warn in strict mode if getDerivedStateFromError is
// not defined.
markLegacyErrorBoundaryAsFailed(this);

// Only log here if componentDidCatch is the only error boundary method defined
logCapturedError(fiber, errorInfo);
}
const error = errorInfo.value;
const stack = errorInfo.stack;
Expand All @@ -150,10 +153,6 @@ function createClassErrorUpdate(
}
}
};
} else if (__DEV__) {
update.callback = () => {
markFailedErrorBoundaryForHotReloading(fiber);
};
}
return update;
}
Expand Down
15 changes: 7 additions & 8 deletions packages/react-reconciler/src/ReactFiberThrow.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ function createClassErrorUpdate(
if (typeof getDerivedStateFromError === 'function') {
const error = errorInfo.value;
update.payload = () => {
logCapturedError(fiber, errorInfo);
return getDerivedStateFromError(error);
};
update.callback = () => {
if (__DEV__) {
markFailedErrorBoundaryForHotReloading(fiber);
}
logCapturedError(fiber, errorInfo);
};
}

const inst = fiber.stateNode;
Expand All @@ -119,16 +124,14 @@ function createClassErrorUpdate(
if (__DEV__) {
markFailedErrorBoundaryForHotReloading(fiber);
}
logCapturedError(fiber, errorInfo);
if (typeof getDerivedStateFromError !== 'function') {
// To preserve the preexisting retry behavior of error boundaries,
// we keep track of which ones already failed during this batch.
// This gets reset before we yield back to the browser.
// TODO: Warn in strict mode if getDerivedStateFromError is
// not defined.
markLegacyErrorBoundaryAsFailed(this);

// Only log here if componentDidCatch is the only error boundary method defined
logCapturedError(fiber, errorInfo);
}
const error = errorInfo.value;
const stack = errorInfo.stack;
Expand All @@ -150,10 +153,6 @@ function createClassErrorUpdate(
}
}
};
} else if (__DEV__) {
update.callback = () => {
markFailedErrorBoundaryForHotReloading(fiber);
};
}
return update;
}
Expand Down

0 comments on commit d1a3291

Please sign in to comment.