Skip to content

Commit 09ed033

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 89007fe commit 09ed033

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
@@ -671,7 +671,6 @@ function executeField(
671671
path: Path,
672672
asyncPayloadRecord?: AsyncPayloadRecord,
673673
): PromiseOrValue<unknown> {
674-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
675674
const fieldName = fieldNodes[0].name.value;
676675
const fieldDef = exeContext.schema.getField(parentType, fieldName);
677676
if (!fieldDef) {
@@ -708,8 +707,14 @@ function executeField(
708707

709708
result = resolveFn(source, args, contextValue, info);
710709
} catch (rawError) {
711-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
712-
const handledError = handleFieldError(error, returnType, errors);
710+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
711+
const handledError = addError(
712+
rawError,
713+
fieldNodes,
714+
returnType,
715+
path,
716+
errors,
717+
);
713718
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
714719
return handledError;
715720
}
@@ -764,11 +769,15 @@ export function buildResolveInfo(
764769
};
765770
}
766771

767-
function handleFieldError(
768-
error: GraphQLError,
772+
function addError(
773+
rawError: unknown,
774+
fieldNodes: ReadonlyArray<FieldNode>,
769775
returnType: GraphQLOutputType,
776+
path: Path,
770777
errors: Array<GraphQLError>,
771778
): null {
779+
const error = locatedError(rawError, fieldNodes, pathToArray(path));
780+
772781
// If the field type is non-nullable, then it is resolved without any
773782
// protection from errors, however it still properly locates the error.
774783
if (isNonNullType(returnType)) {
@@ -922,8 +931,13 @@ async function completePromiseCatchingErrors(
922931
return completed;
923932
} catch (rawError) {
924933
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
925-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
926-
const handledError = handleFieldError(error, returnType, errors);
934+
const handledError = addError(
935+
rawError,
936+
fieldNodes,
937+
returnType,
938+
path,
939+
errors,
940+
);
927941
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
928942
return handledError;
929943
}
@@ -951,8 +965,13 @@ function completeValueCatchingErrors(
951965
);
952966
} catch (rawError) {
953967
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
954-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
955-
const handledError = handleFieldError(error, returnType, errors);
968+
const handledError = addError(
969+
rawError,
970+
fieldNodes,
971+
returnType,
972+
path,
973+
errors,
974+
);
956975
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
957976
return handledError;
958977
}
@@ -962,8 +981,13 @@ function completeValueCatchingErrors(
962981
// to take a second callback for the error case.
963982
completedValue = completedValue.then(undefined, (rawError) => {
964983
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
965-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
966-
const handledError = handleFieldError(error, returnType, errors);
984+
const handledError = addError(
985+
rawError,
986+
fieldNodes,
987+
returnType,
988+
path,
989+
errors,
990+
);
967991
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
968992
return handledError;
969993
});
@@ -1069,8 +1093,9 @@ async function completeAsyncIteratorValue(
10691093
// eslint-disable-next-line no-await-in-loop
10701094
iteration = await iterator.next();
10711095
} catch (rawError) {
1072-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1073-
completedResults.push(handleFieldError(error, itemType, errors));
1096+
completedResults.push(
1097+
addError(rawError, fieldNodes, itemType, itemPath, errors),
1098+
);
10741099
break;
10751100
}
10761101

@@ -2016,8 +2041,13 @@ async function executeStreamAsyncIteratorItem(
20162041
try {
20172042
iteration = await iterator.next();
20182043
} catch (rawError) {
2019-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
2020-
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
2044+
const value = addError(
2045+
rawError,
2046+
fieldNodes,
2047+
itemType,
2048+
itemPath,
2049+
asyncPayloadRecord.errors,
2050+
);
20212051
// don't continue if iterator throws
20222052
return { done: true, value };
20232053
}

0 commit comments

Comments
 (0)