diff --git a/packages/core/lib/process-request.ts b/packages/core/lib/process-request.ts index e798ea4a..34de19de 100644 --- a/packages/core/lib/process-request.ts +++ b/packages/core/lib/process-request.ts @@ -78,6 +78,11 @@ export const processRequest = async ( 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; @@ -258,7 +263,7 @@ export const processRequest = async ( } } catch (error) { const payload: ExecutionResult = { - 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) { diff --git a/packages/core/lib/types.ts b/packages/core/lib/types.ts index 1128c10b..ebea1e2d 100644 --- a/packages/core/lib/types.ts +++ b/packages/core/lib/types.ts @@ -131,6 +131,10 @@ export interface ProcessRequestOptions { * 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 {