Skip to content

Commit

Permalink
introduce handleRawError
Browse files Browse the repository at this point in the history
to include filtering
  • Loading branch information
yaacovCR committed Oct 1, 2022
1 parent 4450aa5 commit 7575d65
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,16 +707,14 @@ function executeField(

result = resolveFn(source, args, contextValue, info);
} catch (rawError) {
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
const handledError = addError(
handleRawError(
rawError,
exeContext,
fieldNodes,
returnType,
path,
errors,
asyncPayloadRecord,
);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
}

if (isPromise(result)) {
Expand Down Expand Up @@ -751,32 +749,32 @@ function completePromiseCatchingErrors(
result: Promise<unknown>,
asyncPayloadRecord?: AsyncPayloadRecord,
): Promise<unknown> {
return result
.then((resolved) =>
completeValue(
exeContext,
returnType,
fieldNodes,
info,
path,
resolved,
asyncPayloadRecord,
),
)
.then(undefined, (rawError) => {
return (
result
.then((resolved) =>
completeValue(
exeContext,
returnType,
fieldNodes,
info,
path,
resolved,
asyncPayloadRecord,
),
)
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
const handledError = addError(
rawError,
fieldNodes,
returnType,
path,
errors,
);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
});
.then(undefined, (rawError) =>
handleRawError(
rawError,
exeContext,
fieldNodes,
returnType,
path,
asyncPayloadRecord,
),
)
);
}

function completeValueCatchingErrors(
Expand All @@ -800,33 +798,29 @@ function completeValueCatchingErrors(
asyncPayloadRecord,
);
} catch (rawError) {
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
const handledError = addError(
return handleRawError(
rawError,
exeContext,
fieldNodes,
returnType,
path,
errors,
asyncPayloadRecord,
);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
}

if (isPromise(completedItem)) {
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
completedItem = completedItem.then(undefined, (rawError) => {
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
const handledError = addError(
completedItem = completedItem.then(undefined, (rawError) =>
handleRawError(
rawError,
exeContext,
fieldNodes,
returnType,
path,
errors,
);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
});
asyncPayloadRecord,
),
);
}
return completedItem;
}
Expand Down Expand Up @@ -858,6 +852,20 @@ export function buildResolveInfo(
};
}

function handleRawError(
rawError: unknown,
exeContext: ExecutionContext,
fieldNodes: ReadonlyArray<FieldNode>,
returnType: GraphQLOutputType,
path: Path,
asyncPayloadRecord: AsyncPayloadRecord | undefined,
): null {
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
addError(rawError, fieldNodes, returnType, path, errors);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
}

function addError(
rawError: unknown,
fieldNodes: ReadonlyArray<FieldNode>,
Expand Down

0 comments on commit 7575d65

Please sign in to comment.