Skip to content

Commit e99bfd4

Browse files
committed
Add fatal error handling
1 parent 52986c3 commit e99bfd4

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

packages/react-server-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {resolveModelToJSON} from 'react-server/src/ReactFlightServer';
2626
import {
2727
emitRow,
2828
resolveModuleMetaData as resolveModuleMetaDataImpl,
29+
close,
2930
} from 'ReactFlightDOMRelayServerIntegration';
3031

3132
export type {
@@ -146,4 +147,8 @@ export function writeChunk(destination: Destination, chunk: Chunk): boolean {
146147

147148
export function completeWriting(destination: Destination) {}
148149

149-
export {close} from 'ReactFlightDOMRelayServerIntegration';
150+
export {close};
151+
152+
export function closeWithError(destination: Destination, error: mixed): void {
153+
close(destination);
154+
}

packages/react-server-native-relay/src/ReactFlightNativeRelayServerHostConfig.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {resolveModelToJSON} from 'react-server/src/ReactFlightServer';
2525

2626
import {
2727
emitRow,
28+
close,
2829
resolveModuleMetaData as resolveModuleMetaDataImpl,
2930
} from 'ReactFlightNativeRelayServerIntegration';
3031

@@ -146,4 +147,8 @@ export function writeChunk(destination: Destination, chunk: Chunk): boolean {
146147

147148
export function completeWriting(destination: Destination) {}
148149

149-
export {close} from 'ReactFlightNativeRelayServerIntegration';
150+
export {close};
151+
152+
export function closeWithError(destination: Destination, error: mixed): void {
153+
close(destination);
154+
}

packages/react-server/src/ReactFlightServer.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
completeWriting,
2525
flushBuffered,
2626
close,
27+
closeWithError,
2728
processModelChunk,
2829
processModuleChunk,
2930
processSymbolChunk,
@@ -599,6 +600,11 @@ function reportError(request: Request, error: mixed): void {
599600
request.onError(error);
600601
}
601602

603+
function fatalError(request: Request, error: mixed): void {
604+
// This is called outside error handling code such as if an error happens in React internals.
605+
closeWithError(request.destination, error);
606+
}
607+
602608
function emitErrorChunk(request: Request, id: number, error: mixed): void {
603609
// TODO: We should not leak error messages to the client in prod.
604610
// Give this an error code instead and log on the server.
@@ -677,18 +683,23 @@ function performWork(request: Request): void {
677683
ReactCurrentDispatcher.current = Dispatcher;
678684
currentCache = request.cache;
679685

680-
const pingedSegments = request.pingedSegments;
681-
request.pingedSegments = [];
682-
for (let i = 0; i < pingedSegments.length; i++) {
683-
const segment = pingedSegments[i];
684-
retrySegment(request, segment);
685-
}
686-
if (request.flowing) {
687-
flushCompletedChunks(request);
686+
try {
687+
const pingedSegments = request.pingedSegments;
688+
request.pingedSegments = [];
689+
for (let i = 0; i < pingedSegments.length; i++) {
690+
const segment = pingedSegments[i];
691+
retrySegment(request, segment);
692+
}
693+
if (request.flowing) {
694+
flushCompletedChunks(request);
695+
}
696+
} catch (error) {
697+
reportError(request, error);
698+
fatalError(request, error);
699+
} finally {
700+
ReactCurrentDispatcher.current = prevDispatcher;
701+
currentCache = prevCache;
688702
}
689-
690-
ReactCurrentDispatcher.current = prevDispatcher;
691-
currentCache = prevCache;
692703
}
693704

694705
let reentrant = false;
@@ -760,7 +771,12 @@ export function startWork(request: Request): void {
760771

761772
export function startFlowing(request: Request): void {
762773
request.flowing = true;
763-
flushCompletedChunks(request);
774+
try {
775+
flushCompletedChunks(request);
776+
} catch (error) {
777+
reportError(request, error);
778+
fatalError(request, error);
779+
}
764780
}
765781

766782
function unsupportedHook(): void {

packages/react-server/src/ReactFlightServerConfigStream.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,5 @@ export {
126126
writeChunk,
127127
completeWriting,
128128
close,
129+
closeWithError,
129130
} from './ReactServerStreamConfig';

0 commit comments

Comments
 (0)