Skip to content

Commit e67eacd

Browse files
committed
Unify error branches under erroredTask
1 parent 8f3035a commit e67eacd

File tree

1 file changed

+42
-105
lines changed

1 file changed

+42
-105
lines changed

packages/react-server/src/ReactFlightServer.js

Lines changed: 42 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -629,21 +629,7 @@ function serializeThenable(
629629
}
630630
case 'rejected': {
631631
const x = thenable.reason;
632-
if (
633-
enablePostpone &&
634-
typeof x === 'object' &&
635-
x !== null &&
636-
(x: any).$$typeof === REACT_POSTPONE_TYPE
637-
) {
638-
const postponeInstance: Postpone = (x: any);
639-
logPostpone(request, postponeInstance.message, newTask);
640-
emitPostponeChunk(request, newTask.id, postponeInstance);
641-
} else {
642-
const digest = logRecoverableError(request, x, null);
643-
emitErrorChunk(request, newTask.id, digest, x);
644-
}
645-
newTask.status = ERRORED;
646-
request.abortableTasks.delete(newTask);
632+
erroredTask(request, newTask, x);
647633
return newTask.id;
648634
}
649635
default: {
@@ -698,21 +684,7 @@ function serializeThenable(
698684
// We expect that the only status it might be otherwise is ABORTED.
699685
// When we abort we emit chunks in each pending task slot and don't need
700686
// to do so again here.
701-
if (
702-
enablePostpone &&
703-
typeof reason === 'object' &&
704-
reason !== null &&
705-
(reason: any).$$typeof === REACT_POSTPONE_TYPE
706-
) {
707-
const postponeInstance: Postpone = (reason: any);
708-
logPostpone(request, postponeInstance.message, newTask);
709-
emitPostponeChunk(request, newTask.id, postponeInstance);
710-
} else {
711-
const digest = logRecoverableError(request, reason, newTask);
712-
emitErrorChunk(request, newTask.id, digest, reason);
713-
}
714-
newTask.status = ERRORED;
715-
request.abortableTasks.delete(newTask);
687+
erroredTask(request, newTask, reason);
716688
enqueueFlush(request);
717689
}
718690
},
@@ -795,8 +767,7 @@ function serializeReadableStream(
795767
}
796768
aborted = true;
797769
request.abortListeners.delete(abortStream);
798-
const digest = logRecoverableError(request, reason, streamTask);
799-
emitErrorChunk(request, streamTask.id, digest, reason);
770+
erroredTask(request, streamTask, reason);
800771
enqueueFlush(request);
801772

802773
// $FlowFixMe should be able to pass mixed
@@ -810,22 +781,10 @@ function serializeReadableStream(
810781
request.abortListeners.delete(abortStream);
811782
if (enableHalt && request.type === PRERENDER) {
812783
request.pendingChunks--;
813-
} else if (
814-
enablePostpone &&
815-
typeof reason === 'object' &&
816-
reason !== null &&
817-
(reason: any).$$typeof === REACT_POSTPONE_TYPE
818-
) {
819-
const postponeInstance: Postpone = (reason: any);
820-
logPostpone(request, postponeInstance.message, streamTask);
821-
emitPostponeChunk(request, streamTask.id, postponeInstance);
822-
enqueueFlush(request);
823784
} else {
824-
const digest = logRecoverableError(request, reason, streamTask);
825-
emitErrorChunk(request, streamTask.id, digest, reason);
785+
erroredTask(request, streamTask, reason);
826786
enqueueFlush(request);
827787
}
828-
829788
// $FlowFixMe should be able to pass mixed
830789
reader.cancel(reason).then(error, error);
831790
}
@@ -931,8 +890,7 @@ function serializeAsyncIterable(
931890
}
932891
aborted = true;
933892
request.abortListeners.delete(abortIterable);
934-
const digest = logRecoverableError(request, reason, streamTask);
935-
emitErrorChunk(request, streamTask.id, digest, reason);
893+
erroredTask(request, streamTask, reason);
936894
enqueueFlush(request);
937895
if (typeof (iterator: any).throw === 'function') {
938896
// The iterator protocol doesn't necessarily include this but a generator do.
@@ -948,19 +906,8 @@ function serializeAsyncIterable(
948906
request.abortListeners.delete(abortIterable);
949907
if (enableHalt && request.type === PRERENDER) {
950908
request.pendingChunks--;
951-
} else if (
952-
enablePostpone &&
953-
typeof reason === 'object' &&
954-
reason !== null &&
955-
(reason: any).$$typeof === REACT_POSTPONE_TYPE
956-
) {
957-
const postponeInstance: Postpone = (reason: any);
958-
logPostpone(request, postponeInstance.message, streamTask);
959-
emitPostponeChunk(request, streamTask.id, postponeInstance);
960-
enqueueFlush(request);
961909
} else {
962-
const digest = logRecoverableError(request, reason, streamTask);
963-
emitErrorChunk(request, streamTask.id, digest, reason);
910+
erroredTask(request, streamTask, reason);
964911
enqueueFlush(request);
965912
}
966913
if (typeof (iterator: any).throw === 'function') {
@@ -2269,8 +2216,7 @@ function serializeBlob(request: Request, blob: Blob): string {
22692216
}
22702217
aborted = true;
22712218
request.abortListeners.delete(abortBlob);
2272-
const digest = logRecoverableError(request, reason, newTask);
2273-
emitErrorChunk(request, newTask.id, digest, reason);
2219+
erroredTask(request, newTask, reason);
22742220
enqueueFlush(request);
22752221
// $FlowFixMe should be able to pass mixed
22762222
reader.cancel(reason).then(error, error);
@@ -2283,19 +2229,8 @@ function serializeBlob(request: Request, blob: Blob): string {
22832229
request.abortListeners.delete(abortBlob);
22842230
if (enableHalt && request.type === PRERENDER) {
22852231
request.pendingChunks--;
2286-
} else if (
2287-
enablePostpone &&
2288-
typeof reason === 'object' &&
2289-
reason !== null &&
2290-
(reason: any).$$typeof === REACT_POSTPONE_TYPE
2291-
) {
2292-
const postponeInstance: Postpone = (reason: any);
2293-
logPostpone(request, postponeInstance.message, newTask);
2294-
emitPostponeChunk(request, newTask.id, postponeInstance);
2295-
enqueueFlush(request);
22962232
} else {
2297-
const digest = logRecoverableError(request, reason, newTask);
2298-
emitErrorChunk(request, newTask.id, digest, reason);
2233+
erroredTask(request, newTask, reason);
22992234
enqueueFlush(request);
23002235
}
23012236
// $FlowFixMe should be able to pass mixed
@@ -2396,24 +2331,6 @@ function renderModel(
23962331
return serializeLazyID(newTask.id);
23972332
}
23982333
return serializeByValueID(newTask.id);
2399-
} else if (enablePostpone && x.$$typeof === REACT_POSTPONE_TYPE) {
2400-
// Something postponed. We'll still send everything we have up until this point.
2401-
// We'll replace this element with a lazy reference that postpones on the client.
2402-
const postponeInstance: Postpone = (x: any);
2403-
request.pendingChunks++;
2404-
const postponeId = request.nextChunkId++;
2405-
logPostpone(request, postponeInstance.message, task);
2406-
emitPostponeChunk(request, postponeId, postponeInstance);
2407-
2408-
// Restore the context. We assume that this will be restored by the inner
2409-
// functions in case nothing throws so we don't use "finally" here.
2410-
task.keyPath = prevKeyPath;
2411-
task.implicitSlot = prevImplicitSlot;
2412-
2413-
if (wasReactNode) {
2414-
return serializeLazyID(postponeId);
2415-
}
2416-
return serializeByValueID(postponeId);
24172334
}
24182335
}
24192336

@@ -2425,8 +2342,21 @@ function renderModel(
24252342
// Something errored. We'll still send everything we have up until this point.
24262343
request.pendingChunks++;
24272344
const errorId = request.nextChunkId++;
2428-
const digest = logRecoverableError(request, x, task);
2429-
emitErrorChunk(request, errorId, digest, x);
2345+
if (
2346+
enablePostpone &&
2347+
typeof x === 'object' &&
2348+
x !== null &&
2349+
x.$$typeof === REACT_POSTPONE_TYPE
2350+
) {
2351+
// Something postponed. We'll still send everything we have up until this point.
2352+
// We'll replace this element with a lazy reference that postpones on the client.
2353+
const postponeInstance: Postpone = (x: any);
2354+
logPostpone(request, postponeInstance.message, task);
2355+
emitPostponeChunk(request, errorId, postponeInstance);
2356+
} else {
2357+
const digest = logRecoverableError(request, x, task);
2358+
emitErrorChunk(request, errorId, digest, x);
2359+
}
24302360
if (wasReactNode) {
24312361
// We'll replace this element with a lazy reference that throws on the client
24322362
// once it gets rendered.
@@ -3946,6 +3876,24 @@ function emitChunk(
39463876
emitModelChunk(request, task.id, json);
39473877
}
39483878

3879+
function erroredTask(request: Request, task: Task, error: mixed): void {
3880+
request.abortableTasks.delete(task);
3881+
task.status = ERRORED;
3882+
if (
3883+
enablePostpone &&
3884+
typeof error === 'object' &&
3885+
error !== null &&
3886+
error.$$typeof === REACT_POSTPONE_TYPE
3887+
) {
3888+
const postponeInstance: Postpone = (error: any);
3889+
logPostpone(request, postponeInstance.message, task);
3890+
emitPostponeChunk(request, task.id, postponeInstance);
3891+
} else {
3892+
const digest = logRecoverableError(request, error, task);
3893+
emitErrorChunk(request, task.id, digest, error);
3894+
}
3895+
}
3896+
39493897
const emptyRoot = {};
39503898

39513899
function retryTask(request: Request, task: Task): void {
@@ -4065,20 +4013,9 @@ function retryTask(request: Request, task: Task): void {
40654013
const ping = task.ping;
40664014
x.then(ping, ping);
40674015
return;
4068-
} else if (enablePostpone && x.$$typeof === REACT_POSTPONE_TYPE) {
4069-
request.abortableTasks.delete(task);
4070-
task.status = ERRORED;
4071-
const postponeInstance: Postpone = (x: any);
4072-
logPostpone(request, postponeInstance.message, task);
4073-
emitPostponeChunk(request, task.id, postponeInstance);
4074-
return;
40754016
}
40764017
}
4077-
4078-
request.abortableTasks.delete(task);
4079-
task.status = ERRORED;
4080-
const digest = logRecoverableError(request, x, task);
4081-
emitErrorChunk(request, task.id, digest, x);
4018+
erroredTask(request, task, x);
40824019
} finally {
40834020
if (__DEV__) {
40844021
debugID = prevDebugID;

0 commit comments

Comments
 (0)