mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Recover from errors with a boundary in completion phase (#14104)
* Recover from errors with a boundary in completion phase * Use a separate field for completing unit of work * Use a simpler fix with one boolean * Reoder conditions * Clarify which paths are DEV-only * Move duplicated line out * Make it clearer this code is DEV-only
This commit is contained in:
1 parent
b020fb1148
commit
3c69a18814
2 files changed
+39
-4
No files matched your search
@@ -2149,4 +2149,19 @@ describe('ReactErrorBoundaries', () => {
|
||||
expect(componentDidCatchError).toBe(thrownError);
|
||||
expect(getDerivedStateFromErrorError).toBe(thrownError);
|
||||
});
|
||||
|
||||
it('should catch errors from invariants in completion phase', () => {
|
||||
const container = document.createElement('div');
|
||||
ReactDOM.render(
|
||||
<ErrorBoundary>
|
||||
<input>
|
||||
<div />
|
||||
</input>
|
||||
</ErrorBoundary>,
|
||||
container,
|
||||
);
|
||||
expect(container.textContent).toContain(
|
||||
'Caught an error: input is a void element tag',
|
||||
);
|
||||
});
|
||||
});
|
||||
+24
-4
@@ -275,11 +275,13 @@ let interruptedBy: Fiber | null = null;
|
||||
|
||||
let stashedWorkInProgressProperties;
|
||||
let replayUnitOfWork;
|
||||
let mayReplayFailedUnitOfWork;
|
||||
let isReplayingFailedUnitOfWork;
|
||||
let originalReplayError;
|
||||
let rethrowOriginalError;
|
||||
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
|
||||
stashedWorkInProgressProperties = null;
|
||||
mayReplayFailedUnitOfWork = true;
|
||||
isReplayingFailedUnitOfWork = false;
|
||||
originalReplayError = null;
|
||||
replayUnitOfWork = (
|
||||
@@ -952,18 +954,22 @@ function completeUnitOfWork(workInProgress: Fiber): Fiber | null {
|
||||
const siblingFiber = workInProgress.sibling;
|
||||
|
||||
if ((workInProgress.effectTag & Incomplete) === NoEffect) {
|
||||
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
|
||||
// Don't replay if it fails during completion phase.
|
||||
mayReplayFailedUnitOfWork = false;
|
||||
}
|
||||
// This fiber completed.
|
||||
// Remember we're completing this unit so we can find a boundary if it fails.
|
||||
nextUnitOfWork = workInProgress;
|
||||
if (enableProfilerTimer) {
|
||||
if (workInProgress.mode & ProfileMode) {
|
||||
startProfilerTimer(workInProgress);
|
||||
}
|
||||
|
||||
nextUnitOfWork = completeWork(
|
||||
current,
|
||||
workInProgress,
|
||||
nextRenderExpirationTime,
|
||||
);
|
||||
|
||||
if (workInProgress.mode & ProfileMode) {
|
||||
// Update render duration assuming we didn't error.
|
||||
stopProfilerTimerIfRunningAndRecordDelta(workInProgress, false);
|
||||
@@ -975,6 +981,10 @@ function completeUnitOfWork(workInProgress: Fiber): Fiber | null {
|
||||
nextRenderExpirationTime,
|
||||
);
|
||||
}
|
||||
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
|
||||
// We're out of completion phase so replaying is fine now.
|
||||
mayReplayFailedUnitOfWork = true;
|
||||
}
|
||||
stopWorkTimer(workInProgress);
|
||||
resetChildExpirationTime(workInProgress, nextRenderExpirationTime);
|
||||
if (__DEV__) {
|
||||
@@ -1292,6 +1302,14 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
|
||||
resetContextDependences();
|
||||
resetHooks();
|
||||
|
||||
// Reset in case completion throws.
|
||||
// This is only used in DEV and when replaying is on.
|
||||
let mayReplay;
|
||||
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
|
||||
mayReplay = mayReplayFailedUnitOfWork;
|
||||
mayReplayFailedUnitOfWork = true;
|
||||
}
|
||||
|
||||
if (nextUnitOfWork === null) {
|
||||
// This is a fatal error.
|
||||
didFatal = true;
|
||||
@@ -1303,9 +1321,11 @@ function renderRoot(root: FiberRoot, isYieldy: boolean): void {
|
||||
(resetCurrentlyProcessingQueue: any)();
|
||||
}
|
||||
|
||||
const failedUnitOfWork: Fiber = nextUnitOfWork;
|
||||
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
|
||||
replayUnitOfWork(failedUnitOfWork, thrownValue, isYieldy);
|
||||
if (mayReplay) {
|
||||
const failedUnitOfWork: Fiber = nextUnitOfWork;
|
||||
replayUnitOfWork(failedUnitOfWork, thrownValue, isYieldy);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: we already know this isn't true in some cases.
|
||||
|
||||
Reference in new issue
Block a user