Skip to content

Commit 9bc7280

Browse files
committed
introduce addError instead of handleFieldError
addError calls locatedError and then adds the error as previously in handleFieldError, throwing if the type is non-null
1 parent 8f30fd8 commit 9bc7280

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

src/execution/execute.ts

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,6 @@ function executeField(
667667
path: Path,
668668
asyncPayloadRecord?: AsyncPayloadRecord,
669669
): PromiseOrValue<unknown> {
670-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
671670
const fieldName = fieldNodes[0].name.value;
672671
const fieldDef = exeContext.schema.getField(parentType, fieldName);
673672
if (!fieldDef) {
@@ -704,8 +703,14 @@ function executeField(
704703

705704
result = resolveFn(source, args, contextValue, info);
706705
} catch (rawError) {
707-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
708-
const handledError = handleFieldError(error, returnType, errors);
706+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
707+
const handledError = addError(
708+
rawError,
709+
fieldNodes,
710+
returnType,
711+
path,
712+
errors,
713+
);
709714
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
710715
return handledError;
711716
}
@@ -760,11 +765,15 @@ export function buildResolveInfo(
760765
};
761766
}
762767

763-
function handleFieldError(
764-
error: GraphQLError,
768+
function addError(
769+
rawError: unknown,
770+
fieldNodes: ReadonlyArray<FieldNode>,
765771
returnType: GraphQLOutputType,
772+
path: Path,
766773
errors: Array<GraphQLError>,
767774
): null {
775+
const error = locatedError(rawError, fieldNodes, pathToArray(path));
776+
768777
// If the field type is non-nullable, then it is resolved without any
769778
// protection from errors, however it still properly locates the error.
770779
if (isNonNullType(returnType)) {
@@ -918,8 +927,13 @@ async function completePromiseCatchingErrors(
918927
return completed;
919928
} catch (rawError) {
920929
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
921-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
922-
const handledError = handleFieldError(error, returnType, errors);
930+
const handledError = addError(
931+
rawError,
932+
fieldNodes,
933+
returnType,
934+
path,
935+
errors,
936+
);
923937
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
924938
return handledError;
925939
}
@@ -947,8 +961,13 @@ function completeValueCatchingErrors(
947961
);
948962
} catch (rawError) {
949963
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
950-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
951-
const handledError = handleFieldError(error, returnType, errors);
964+
const handledError = addError(
965+
rawError,
966+
fieldNodes,
967+
returnType,
968+
path,
969+
errors,
970+
);
952971
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
953972
return handledError;
954973
}
@@ -958,8 +977,13 @@ function completeValueCatchingErrors(
958977
// to take a second callback for the error case.
959978
completedValue = completedValue.then(undefined, (rawError) => {
960979
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
961-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
962-
const handledError = handleFieldError(error, returnType, errors);
980+
const handledError = addError(
981+
rawError,
982+
fieldNodes,
983+
returnType,
984+
path,
985+
errors,
986+
);
963987
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
964988
return handledError;
965989
});
@@ -1065,8 +1089,9 @@ async function completeAsyncIteratorValue(
10651089
// eslint-disable-next-line no-await-in-loop
10661090
iteration = await iterator.next();
10671091
} catch (rawError) {
1068-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1069-
completedResults.push(handleFieldError(error, itemType, errors));
1092+
completedResults.push(
1093+
addError(rawError, fieldNodes, itemType, itemPath, errors),
1094+
);
10701095
break;
10711096
}
10721097

@@ -1957,8 +1982,13 @@ async function executeStreamIteratorItem(
19571982
try {
19581983
iteration = await iterator.next();
19591984
} catch (rawError) {
1960-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1961-
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1985+
const value = addError(
1986+
rawError,
1987+
fieldNodes,
1988+
itemType,
1989+
itemPath,
1990+
asyncPayloadRecord.errors,
1991+
);
19621992
// don't continue if iterator throws
19631993
return { done: true, value };
19641994
}

0 commit comments

Comments
 (0)