Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Add onError option to processRequest #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/core/lib/process-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export const processRequest = async <TContext = {}, TRootValue = {}>(
validate = defaultValidate,
validationRules,
variables,
onError = (error: Error) => {
// eslint-disable-next-line no-console
console.error(error);
return "Internal Server Error";
},
} = options;

let context: TContext | undefined;
Expand Down Expand Up @@ -258,7 +263,7 @@ export const processRequest = async <TContext = {}, TRootValue = {}>(
}
} catch (error) {
const payload: ExecutionResult<any> = {
errors: ((error as HttpError).graphqlErrors as GraphQLError[]) || [new GraphQLError((error as Error).message)],
errors: ((error as HttpError).graphqlErrors as GraphQLError[]) || [new GraphQLError(onError(error as Error))],
};

if (isEventStream) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export interface ProcessRequestOptions<TContext, TRootValue> {
* Values for any Variables defined by the Operation.
*/
variables?: string | { [name: string]: any };
/**
* An optional function which will be used to format any unexpected errors that occur during execution.
*/
onError?: (error: Error) => string;
}

export interface FormatPayloadParams<TContext, TRootValue> {
Expand Down