Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fiber] Create virtual Fiber when an error occurs during reconcilation #29804

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 23 additions & 45 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -964,67 +964,47 @@ describe('ReactFlight', () => {
const testCases = (
<>
<ClientErrorBoundary expectedMessage="This is a real Error.">
<div>
<Throw value={new TypeError('This is a real Error.')} />
</div>
<Throw value={new TypeError('This is a real Error.')} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="This is a string error.">
<div>
<Throw value="This is a string error." />
</div>
<Throw value="This is a string error." />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="{message: ..., extra: ..., nested: ...}">
<div>
<Throw
value={{
message: 'This is a long message',
extra: 'properties',
nested: {more: 'prop'},
}}
/>
</div>
<Throw
value={{
message: 'This is a long message',
extra: 'properties',
nested: {more: 'prop'},
}}
/>
</ClientErrorBoundary>
<ClientErrorBoundary
expectedMessage={'{message: "Short", extra: ..., nested: ...}'}>
<div>
<Throw
value={{
message: 'Short',
extra: 'properties',
nested: {more: 'prop'},
}}
/>
</div>
<Throw
value={{
message: 'Short',
extra: 'properties',
nested: {more: 'prop'},
}}
/>
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="Symbol(hello)">
<div>
<Throw value={Symbol('hello')} />
</div>
<Throw value={Symbol('hello')} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="123">
<div>
<Throw value={123} />
</div>
<Throw value={123} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="undefined">
<div>
<Throw value={undefined} />
</div>
<Throw value={undefined} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="<div/>">
<div>
<Throw value={<div />} />
</div>
<Throw value={<div />} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage="function Foo() {}">
<div>
<Throw value={function Foo() {}} />
</div>
<Throw value={function Foo() {}} />
</ClientErrorBoundary>
<ClientErrorBoundary expectedMessage={'["array"]'}>
<div>
<Throw value={['array']} />
</div>
<Throw value={['array']} />
</ClientErrorBoundary>
<ClientErrorBoundary
expectedMessage={
Expand All @@ -1034,9 +1014,7 @@ describe('ReactFlight', () => {
'- A library pre-bundled an old copy of "react" or "react/jsx-runtime".\n' +
'- A compiler tries to "inline" JSX instead of using the runtime.'
}>
<div>
<LazyInlined />
</div>
<LazyInlined />
</ClientErrorBoundary>
</>
);
Expand Down
11 changes: 11 additions & 0 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export function getInternalReactConstants(version: string): {
TracingMarkerComponent: 25, // Experimental - This is technically in 18 but we don't
// want to fork again so we're adding it here instead
YieldComponent: -1, // Removed
Throw: 29,
};
} else if (gte(version, '17.0.0-alpha')) {
ReactTypeOfWork = {
Expand Down Expand Up @@ -302,6 +303,7 @@ export function getInternalReactConstants(version: string): {
SuspenseListComponent: 19, // Experimental
TracingMarkerComponent: -1, // Doesn't exist yet
YieldComponent: -1, // Removed
Throw: -1, // Doesn't exist yet
};
} else if (gte(version, '16.6.0-beta.0')) {
ReactTypeOfWork = {
Expand Down Expand Up @@ -336,6 +338,7 @@ export function getInternalReactConstants(version: string): {
SuspenseListComponent: 19, // Experimental
TracingMarkerComponent: -1, // Doesn't exist yet
YieldComponent: -1, // Removed
Throw: -1, // Doesn't exist yet
};
} else if (gte(version, '16.4.3-alpha')) {
ReactTypeOfWork = {
Expand Down Expand Up @@ -370,6 +373,7 @@ export function getInternalReactConstants(version: string): {
SuspenseListComponent: -1, // Doesn't exist yet
TracingMarkerComponent: -1, // Doesn't exist yet
YieldComponent: -1, // Removed
Throw: -1, // Doesn't exist yet
};
} else {
ReactTypeOfWork = {
Expand Down Expand Up @@ -404,6 +408,7 @@ export function getInternalReactConstants(version: string): {
SuspenseListComponent: -1, // Doesn't exist yet
TracingMarkerComponent: -1, // Doesn't exist yet
YieldComponent: 9,
Throw: -1, // Doesn't exist yet
};
}
// **********************************************************
Expand Down Expand Up @@ -445,6 +450,7 @@ export function getInternalReactConstants(version: string): {
SuspenseComponent,
SuspenseListComponent,
TracingMarkerComponent,
Throw,
} = ReactTypeOfWork;

function resolveFiberType(type: any): $FlowFixMe {
Expand Down Expand Up @@ -551,6 +557,9 @@ export function getInternalReactConstants(version: string): {
return 'Profiler';
case TracingMarkerComponent:
return 'TracingMarker';
case Throw:
// This should really never be visible.
return 'Error';
default:
const typeSymbol = getTypeSymbol(type);

Expand Down Expand Up @@ -672,6 +681,7 @@ export function attach(
SuspenseComponent,
SuspenseListComponent,
TracingMarkerComponent,
Throw,
} = ReactTypeOfWork;
const {
ImmediatePriority,
Expand Down Expand Up @@ -1036,6 +1046,7 @@ export function attach(
case HostText:
case LegacyHiddenComponent:
case OffscreenComponent:
case Throw:
return true;
case HostRoot:
// It is never valid to filter the root element.
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type WorkTagMap = {
SuspenseListComponent: WorkTag,
TracingMarkerComponent: WorkTag,
YieldComponent: WorkTag,
Throw: WorkTag,
};

// TODO: If it's useful for the frontend to know which types of data an Element has
Expand Down
62 changes: 47 additions & 15 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Forked,
PlacementDEV,
} from './ReactFiberFlags';
import {NoMode, ConcurrentMode} from './ReactTypeOfMode';
import {
getIteratorFn,
ASYNC_ITERATOR,
Expand All @@ -46,6 +47,7 @@ import isArray from 'shared/isArray';
import {
enableRefAsProp,
enableAsyncIterableChildren,
disableLegacyMode,
} from 'shared/ReactFeatureFlags';

import {
Expand All @@ -55,11 +57,16 @@ import {
createFiberFromFragment,
createFiberFromText,
createFiberFromPortal,
createFiberFromThrow,
} from './ReactFiber';
import {isCompatibleFamilyForHotReloading} from './ReactFiberHotReloading';
import {getIsHydrating} from './ReactFiberHydrationContext';
import {pushTreeFork} from './ReactFiberTreeContext';
import {createThenableState, trackUsedThenable} from './ReactFiberThenable';
import {
SuspenseException,
createThenableState,
trackUsedThenable,
} from './ReactFiberThenable';
import {readContextDuringReconciliation} from './ReactFiberNewContext';
import {callLazyInitInDEV} from './ReactFiberCallUserSpace';

Expand Down Expand Up @@ -1919,20 +1926,45 @@ function createChildReconciler(
newChild: any,
lanes: Lanes,
): Fiber | null {
// This indirection only exists so we can reset `thenableState` at the end.
// It should get inlined by Closure.
thenableIndexCounter = 0;
const firstChildFiber = reconcileChildFibersImpl(
returnFiber,
currentFirstChild,
newChild,
lanes,
null, // debugInfo
);
thenableState = null;
// Don't bother to reset `thenableIndexCounter` to 0 because it always gets
// set at the beginning.
return firstChildFiber;
try {
// This indirection only exists so we can reset `thenableState` at the end.
// It should get inlined by Closure.
thenableIndexCounter = 0;
const firstChildFiber = reconcileChildFibersImpl(
returnFiber,
currentFirstChild,
newChild,
lanes,
null, // debugInfo
);
thenableState = null;
// Don't bother to reset `thenableIndexCounter` to 0 because it always gets
// set at the beginning.
return firstChildFiber;
} catch (x) {
if (
x === SuspenseException ||
(!disableLegacyMode &&
(returnFiber.mode & ConcurrentMode) === NoMode &&
typeof x === 'object' &&
x !== null &&
typeof x.then === 'function')
) {
// Suspense exceptions need to read the current suspended state before
// yielding and replay it using the same sequence so this trick doesn't
// work here.
// Suspending in legacy mode actually mounts so if we let the child
// mount then we delete its state in an update.
throw x;
}
// Something errored during reconciliation but it's conceptually a child that
// errored and not the current component itself so we create a virtual child
// that throws in its begin phase. That way the current component can handle
// the error or suspending if needed.
const throwFiber = createFiberFromThrow(x, returnFiber.mode, lanes);
throwFiber.return = returnFiber;
return throwFiber;
}
}

return reconcileChildFibers;
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
OffscreenComponent,
LegacyHiddenComponent,
TracingMarkerComponent,
Throw,
} from './ReactWorkTags';
import {OffscreenVisible} from './ReactFiberActivityComponent';
import {getComponentNameFromOwner} from 'react-reconciler/src/getComponentNameFromFiber';
Expand Down Expand Up @@ -879,3 +880,13 @@ export function createFiberFromPortal(
};
return fiber;
}

export function createFiberFromThrow(
error: mixed,
mode: TypeOfMode,
lanes: Lanes,
): Fiber {
const fiber = createFiber(Throw, error, null, mode);
fiber.lanes = lanes;
return fiber;
}
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
LegacyHiddenComponent,
CacheComponent,
TracingMarkerComponent,
Throw,
} from './ReactWorkTags';
import {
NoFlags,
Expand Down Expand Up @@ -4126,6 +4127,11 @@ function beginWork(
}
break;
}
case Throw: {
// This represents a Component that threw in the reconciliation phase.
// So we'll rethrow here. This might be
throw workInProgress.pendingProps;
}
}

throw new Error(
Expand Down
7 changes: 7 additions & 0 deletions packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
LegacyHiddenComponent,
CacheComponent,
TracingMarkerComponent,
Throw,
} from './ReactWorkTags';
import {NoMode, ConcurrentMode, ProfileMode} from './ReactTypeOfMode';
import {
Expand Down Expand Up @@ -1802,6 +1803,12 @@ function completeWork(
}
return null;
}
case Throw: {
if (!disableLegacyMode) {
// Only Legacy Mode completes an errored node.
return null;
}
}
}

throw new Error(
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactWorkTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export type WorkTag =
| 25
| 26
| 27
| 28;
| 28
| 29;

export const FunctionComponent = 0;
export const ClassComponent = 1;
Expand Down Expand Up @@ -65,3 +66,4 @@ export const TracingMarkerComponent = 25;
export const HostHoistable = 26;
export const HostSingleton = 27;
export const IncompleteFunctionComponent = 28;
export const Throw = 29;