@@ -8,16 +8,17 @@ import type {
88 ExecutionArgs ,
99 ExecutionResult ,
1010 FormattedExecutionResult ,
11- GraphQLError ,
1211 GraphQLSchema ,
1312 GraphQLFieldResolver ,
1413 GraphQLTypeResolver ,
1514 GraphQLFormattedError ,
1615} from 'graphql' ;
1716import accepts from 'accepts' ;
1817import httpError from 'http-errors' ;
18+ import type { HttpError } from 'http-errors' ;
1919import {
2020 Source ,
21+ GraphQLError ,
2122 parse ,
2223 validate ,
2324 execute ,
@@ -206,7 +207,7 @@ export function graphqlHTTP(options: Options): Middleware {
206207 // Parse the Request to get GraphQL request parameters.
207208 try {
208209 params = await getGraphQLParams ( request ) ;
209- } catch ( error ) {
210+ } catch ( error : unknown ) {
210211 // When we failed to parse the GraphQL parameters, we still need to get
211212 // the options object, so make an options call to resolve just that.
212213 const optionsData = await resolveOptions ( ) ;
@@ -284,7 +285,7 @@ export function graphqlHTTP(options: Options): Middleware {
284285 let documentAST ;
285286 try {
286287 documentAST = parseFn ( new Source ( query , 'GraphQL request' ) ) ;
287- } catch ( syntaxError ) {
288+ } catch ( syntaxError : unknown ) {
288289 // Return 400: Bad Request if any syntax errors errors exist.
289290 throw httpError ( 400 , 'GraphQL syntax error.' , {
290291 graphqlErrors : [ syntaxError ] ,
@@ -337,7 +338,7 @@ export function graphqlHTTP(options: Options): Middleware {
337338 fieldResolver,
338339 typeResolver,
339340 } ) ;
340- } catch ( contextError ) {
341+ } catch ( contextError : unknown ) {
341342 // Return 400: Bad Request if any execution context errors exist.
342343 throw httpError ( 400 , 'GraphQL execution context error.' , {
343344 graphqlErrors : [ contextError ] ,
@@ -359,9 +360,15 @@ export function graphqlHTTP(options: Options): Middleware {
359360 result = { ...result , extensions } ;
360361 }
361362 }
362- } catch ( error ) {
363+ } catch ( rawError : unknown ) {
363364 // If an error was caught, report the httpError status, or 500.
364- response . statusCode = error . status ?? 500 ;
365+ const error : HttpError = httpError (
366+ 500 ,
367+ /* istanbul ignore next: Thrown by underlying library. */
368+ rawError instanceof Error ? rawError : String ( rawError ) ,
369+ ) ;
370+
371+ response . statusCode = error . status ;
365372
366373 const { headers } = error ;
367374 if ( headers != null ) {
@@ -370,7 +377,19 @@ export function graphqlHTTP(options: Options): Middleware {
370377 }
371378 }
372379
373- result = { data : undefined , errors : error . graphqlErrors ?? [ error ] } ;
380+ if ( error . graphqlErrors == null ) {
381+ const graphqlError = new GraphQLError (
382+ error . message ,
383+ undefined ,
384+ undefined ,
385+ undefined ,
386+ undefined ,
387+ error ,
388+ ) ;
389+ result = { data : undefined , errors : [ graphqlError ] } ;
390+ } else {
391+ result = { data : undefined , errors : error . graphqlErrors } ;
392+ }
374393 }
375394
376395 // If no data was included in the result, that indicates a runtime query
@@ -482,7 +501,7 @@ export async function getGraphQLParams(
482501 if ( typeof variables === 'string' ) {
483502 try {
484503 variables = JSON . parse ( variables ) ;
485- } catch ( error ) {
504+ } catch {
486505 throw httpError ( 400 , 'Variables are invalid JSON.' ) ;
487506 }
488507 } else if ( typeof variables !== 'object' ) {
0 commit comments