Skip to content

Commit c3398d5

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 91c280d commit c3398d5

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
@@ -668,7 +668,6 @@ function executeField(
668668
path: Path,
669669
asyncPayloadRecord?: AsyncPayloadRecord,
670670
): PromiseOrValue<unknown> {
671-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
672671
const fieldName = fieldNodes[0].name.value;
673672
const fieldDef = exeContext.schema.getField(parentType, fieldName);
674673
if (!fieldDef) {
@@ -705,8 +704,14 @@ function executeField(
705704

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

764-
function handleFieldError(
765-
error: GraphQLError,
769+
function addError(
770+
rawError: unknown,
771+
fieldNodes: ReadonlyArray<FieldNode>,
766772
returnType: GraphQLOutputType,
773+
path: Path,
767774
errors: Array<GraphQLError>,
768775
): null {
776+
const error = locatedError(rawError, fieldNodes, pathToArray(path));
777+
769778
// If the field type is non-nullable, then it is resolved without any
770779
// protection from errors, however it still properly locates the error.
771780
if (isNonNullType(returnType)) {
@@ -919,8 +928,13 @@ async function completePromiseCatchingErrors(
919928
return completed;
920929
} catch (rawError) {
921930
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
922-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
923-
const handledError = handleFieldError(error, returnType, errors);
931+
const handledError = addError(
932+
rawError,
933+
fieldNodes,
934+
returnType,
935+
path,
936+
errors,
937+
);
924938
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
925939
return handledError;
926940
}
@@ -948,8 +962,13 @@ function completeValueCatchingErrors(
948962
);
949963
} catch (rawError) {
950964
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
951-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
952-
const handledError = handleFieldError(error, returnType, errors);
965+
const handledError = addError(
966+
rawError,
967+
fieldNodes,
968+
returnType,
969+
path,
970+
errors,
971+
);
953972
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
954973
return handledError;
955974
}
@@ -959,8 +978,13 @@ function completeValueCatchingErrors(
959978
// to take a second callback for the error case.
960979
completedValue = completedValue.then(undefined, (rawError) => {
961980
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
962-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
963-
const handledError = handleFieldError(error, returnType, errors);
981+
const handledError = addError(
982+
rawError,
983+
fieldNodes,
984+
returnType,
985+
path,
986+
errors,
987+
);
964988
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
965989
return handledError;
966990
});
@@ -1066,8 +1090,9 @@ async function completeAsyncIteratorValue(
10661090
// eslint-disable-next-line no-await-in-loop
10671091
iteration = await iterator.next();
10681092
} catch (rawError) {
1069-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1070-
completedResults.push(handleFieldError(error, itemType, errors));
1093+
completedResults.push(
1094+
addError(rawError, fieldNodes, itemType, itemPath, errors),
1095+
);
10711096
break;
10721097
}
10731098

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

0 commit comments

Comments
 (0)