Skip to content

Commit e8b51ca

Browse files
committed
introduce handleRawError
to include filtering
1 parent 78d7a21 commit e8b51ca

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

src/execution/execute.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -748,17 +748,13 @@ function executeField(
748748
// Note: we don't rely on a `catch` method, but we do expect "thenable"
749749
// to take a second callback for the error case.
750750
return completed.then(undefined, (rawError) => {
751-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
752-
addError(rawError, fieldNodes, returnType, path, errors);
753-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
751+
handleRawError(rawError, exeContext, fieldNodes, returnType, path, asyncPayloadRecord);
754752
return null;
755753
});
756754
}
757755
return completed;
758756
} catch (rawError) {
759-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
760-
addError(rawError, fieldNodes, returnType, path, errors);
761-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
757+
handleRawError(rawError, exeContext, fieldNodes, returnType, path, asyncPayloadRecord);
762758
return null;
763759
}
764760
}
@@ -790,6 +786,19 @@ export function buildResolveInfo(
790786
};
791787
}
792788

789+
function handleRawError(
790+
rawError: unknown,
791+
exeContext: ExecutionContext,
792+
fieldNodes: ReadonlyArray<FieldNode>,
793+
returnType: GraphQLOutputType,
794+
path: Path,
795+
asyncPayloadRecord: AsyncPayloadRecord | undefined,
796+
): void {
797+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
798+
addError(rawError, fieldNodes, returnType, path, errors);
799+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
800+
}
801+
793802
function addError(
794803
rawError: unknown,
795804
fieldNodes: ReadonlyArray<FieldNode>,
@@ -948,9 +957,14 @@ async function completePromisedValue(
948957
}
949958
return completed;
950959
} catch (rawError) {
951-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
952-
addError(rawError, fieldNodes, returnType, path, errors);
953-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
960+
handleRawError(
961+
rawError,
962+
exeContext,
963+
fieldNodes,
964+
returnType,
965+
path,
966+
asyncPayloadRecord,
967+
);
954968
return null;
955969
}
956970
}
@@ -1025,7 +1039,6 @@ async function completeAsyncIteratorValue(
10251039
iterator: AsyncIterator<unknown>,
10261040
asyncPayloadRecord?: AsyncPayloadRecord,
10271041
): Promise<ReadonlyArray<unknown>> {
1028-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
10291042
const stream = getStreamValues(exeContext, fieldNodes, path);
10301043
let containsPromise = false;
10311044
const completedResults: Array<unknown> = [];
@@ -1058,6 +1071,7 @@ async function completeAsyncIteratorValue(
10581071
// eslint-disable-next-line no-await-in-loop
10591072
iteration = await iterator.next();
10601073
} catch (rawError) {
1074+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
10611075
addError(rawError, fieldNodes, itemType, itemPath, errors);
10621076
completedResults.push(null);
10631077
break;
@@ -1071,7 +1085,6 @@ async function completeAsyncIteratorValue(
10711085
completeListItemValue(
10721086
iteration.value,
10731087
completedResults,
1074-
errors,
10751088
exeContext,
10761089
itemType,
10771090
fieldNodes,
@@ -1101,7 +1114,6 @@ function completeListValue(
11011114
asyncPayloadRecord?: AsyncPayloadRecord,
11021115
): PromiseOrValue<ReadonlyArray<unknown>> {
11031116
const itemType = returnType.ofType;
1104-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
11051117

11061118
if (isAsyncIterable(result)) {
11071119
const iterator = result[Symbol.asyncIterator]();
@@ -1160,7 +1172,6 @@ function completeListValue(
11601172
completeListItemValue(
11611173
item,
11621174
completedResults,
1163-
errors,
11641175
exeContext,
11651176
itemType,
11661177
fieldNodes,
@@ -1186,7 +1197,6 @@ function completeListValue(
11861197
function completeListItemValue(
11871198
item: unknown,
11881199
completedResults: Array<unknown>,
1189-
errors: Array<GraphQLError>,
11901200
exeContext: ExecutionContext,
11911201
itemType: GraphQLOutputType,
11921202
fieldNodes: ReadonlyArray<FieldNode>,
@@ -1226,8 +1236,14 @@ function completeListItemValue(
12261236
// to take a second callback for the error case.
12271237
completedResults.push(
12281238
completedItem.then(undefined, (rawError) => {
1229-
addError(rawError, fieldNodes, itemType, itemPath, errors);
1230-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1239+
handleRawError(
1240+
rawError,
1241+
exeContext,
1242+
fieldNodes,
1243+
itemType,
1244+
itemPath,
1245+
asyncPayloadRecord,
1246+
);
12311247
return null;
12321248
}),
12331249
);
@@ -1237,8 +1253,14 @@ function completeListItemValue(
12371253

12381254
completedResults.push(completedItem);
12391255
} catch (rawError) {
1240-
addError(rawError, fieldNodes, itemType, itemPath, errors);
1241-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1256+
handleRawError(
1257+
rawError,
1258+
exeContext,
1259+
fieldNodes,
1260+
itemType,
1261+
itemPath,
1262+
asyncPayloadRecord,
1263+
);
12421264
completedResults.push(null);
12431265
}
12441266

@@ -1853,15 +1875,15 @@ function executeStreamField(
18531875
asyncPayloadRecord,
18541876
);
18551877
} catch (rawError) {
1856-
addError(
1878+
handleRawError(
18571879
rawError,
1880+
exeContext,
18581881
fieldNodes,
18591882
itemType,
18601883
itemPath,
1861-
asyncPayloadRecord.errors,
1884+
asyncPayloadRecord,
18621885
);
18631886
completedItem = null;
1864-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18651887
}
18661888
} catch (error) {
18671889
asyncPayloadRecord.errors.push(error);
@@ -1873,14 +1895,14 @@ function executeStreamField(
18731895
if (isPromise(completedItem)) {
18741896
const completedItems = completedItem
18751897
.then(undefined, (rawError) => {
1876-
addError(
1898+
handleRawError(
18771899
rawError,
1900+
exeContext,
18781901
fieldNodes,
18791902
itemType,
18801903
itemPath,
1881-
asyncPayloadRecord.errors,
1904+
asyncPayloadRecord,
18821905
);
1883-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18841906
return null;
18851907
})
18861908
.then(
@@ -1945,27 +1967,27 @@ async function executeStreamIteratorItem(
19451967

19461968
if (isPromise(completedItem)) {
19471969
completedItem = completedItem.then(undefined, (rawError) => {
1948-
addError(
1970+
handleRawError(
19491971
rawError,
1972+
exeContext,
19501973
fieldNodes,
19511974
itemType,
19521975
itemPath,
1953-
asyncPayloadRecord.errors,
1976+
asyncPayloadRecord,
19541977
);
1955-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19561978
return null;
19571979
});
19581980
}
19591981
return { done: false, value: completedItem };
19601982
} catch (rawError) {
1961-
addError(
1983+
handleRawError(
19621984
rawError,
1985+
exeContext,
19631986
fieldNodes,
19641987
itemType,
19651988
itemPath,
1966-
asyncPayloadRecord.errors,
1989+
asyncPayloadRecord,
19671990
);
1968-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19691991
return { done: false, value: null };
19701992
}
19711993
}

0 commit comments

Comments
 (0)