Skip to content

Commit

Permalink
Add fatal error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 29, 2021
1 parent e6c2218 commit f1cd2ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ export function writeChunk(destination: Destination, chunk: Chunk): boolean {
export function completeWriting(destination: Destination) {}

export {close} from 'ReactFlightDOMRelayServerIntegration';
export {close as closeWithError} from 'ReactFlightDOMRelayServerIntegration';
40 changes: 28 additions & 12 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
completeWriting,
flushBuffered,
close,
closeWithError,
processModelChunk,
processModuleChunk,
processSymbolChunk,
Expand Down Expand Up @@ -599,6 +600,11 @@ function reportError(request: Request, error: mixed): void {
request.onError(error);
}

function fatalError(request: Request, error: mixed): void {
// This is called outside error handling code such as if an error happens in React internals.
closeWithError(request.destination, error);
}

function emitErrorChunk(request: Request, id: number, error: mixed): void {
// TODO: We should not leak error messages to the client in prod.
// Give this an error code instead and log on the server.
Expand Down Expand Up @@ -677,18 +683,23 @@ function performWork(request: Request): void {
ReactCurrentDispatcher.current = Dispatcher;
currentCache = request.cache;

const pingedSegments = request.pingedSegments;
request.pingedSegments = [];
for (let i = 0; i < pingedSegments.length; i++) {
const segment = pingedSegments[i];
retrySegment(request, segment);
}
if (request.flowing) {
flushCompletedChunks(request);
try {
const pingedSegments = request.pingedSegments;
request.pingedSegments = [];
for (let i = 0; i < pingedSegments.length; i++) {
const segment = pingedSegments[i];
retrySegment(request, segment);
}
if (request.flowing) {
flushCompletedChunks(request);
}
} catch (error) {
reportError(request, error);
fatalError(request, error);
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
currentCache = prevCache;
}

ReactCurrentDispatcher.current = prevDispatcher;
currentCache = prevCache;
}

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

export function startFlowing(request: Request): void {
request.flowing = true;
flushCompletedChunks(request);
try {
flushCompletedChunks(request);
} catch (error) {
reportError(request, error);
fatalError(request, error);
}
}

function unsupportedHook(): void {
Expand Down
1 change: 1 addition & 0 deletions packages/react-server/src/ReactFlightServerConfigStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ export {
writeChunk,
completeWriting,
close,
closeWithError,
} from './ReactServerStreamConfig';

0 comments on commit f1cd2ef

Please sign in to comment.