fix: throwing error messages (#148)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 769c2e6 · Browse files · 2 files·+43 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+43 added−15 removed
- tests:+0 added−1 removed
- other:+43 added−14 removed
Provenance
- Commit
107aece- Tree
6eed8cb- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
769c2e6
2 files changed+43 added−15 removed
modified:src/middleware/graphql-server/graphql-server.test.ts-1 removed
544 unmodified lines545 query: 'query helloWho($who: String){ test(who: $who) }',546 variables: { who: ['John', 'Jane'] },547 }),548 })549-549 expect(res.status).toBe(500)550 })551 552 it('Handles unsupported HTTP methods', async () => {11 unmodified linesmodified:src/middleware/graphql-server/graphql-server.ts+43 added-14 removed
10 unmodified lines11 validateSchema,12 validate,13 specifiedRules,14 getOperationAST,15+ GraphQLError,16 } from 'graphql'17 17-import type { GraphQLSchema, DocumentNode, ValidationRule, FormattedExecutionResult } from 'graphql'18+import type {19+ GraphQLSchema,20+ DocumentNode,21+ ValidationRule,22+ FormattedExecutionResult,23+ GraphQLFormattedError,24+} from 'graphql'25 26 type Options = {27 schema: GraphQLSchema28 rootValue?: unknown24 unmodified lines53 try {54 params = await getGraphQLParams(c.req)55 } catch (e) {56 if (e instanceof Error) {50- c.res = c.json(errorMessages([e.message]), 400)57+ c.res = c.json(errorMessages([e.message], [e]), 400)58 }59 return60 }61 6 unmodified lines68 69 const schemaValidationErrors = validateSchema(schema)70 if (schemaValidationErrors.length > 0) {71 // Return 500: Internal Server Error if invalid schema.65- c.res = c.json(errorMessages(['GraphQL schema validation error.']), 500)72+ c.res = c.json(73+ errorMessages(['GraphQL schema validation error.'], schemaValidationErrors),74+ 50075+ )76 return77 }78 79 let documentAST: DocumentNode80 try {81 documentAST = parse(new Source(query, 'GraphQL request'))82 } catch (syntaxError: unknown) {83 // Return 400: Bad Request if any syntax errors errors exist.84+ if (syntaxError instanceof Error) {85+ const e = new GraphQLError(syntaxError.message, {86+ originalError: syntaxError,87+ })74- c.res = c.json(errorMessages(['GraphQL syntax error.']), 400)88+ c.res = c.json(errorMessages(['GraphQL syntax error.'], [e]), 400)89+ }90 return91 }92 93 // Validate AST, reporting any errors.94 const validationErrors = validate(schema, documentAST, [...specifiedRules, ...validationRules])95 96 if (validationErrors.length > 0) {97 // Return 400: Bad Request if any validation errors exist.83- c.res = c.json(errorMessages(['GraphQL validation error.']), 400)98+ c.res = c.json(errorMessages(['GraphQL validation error.'], validationErrors), 400)99 return100 }101 102 if (c.req.method === 'GET') {28 unmodified lines131 rootValue,132 variableValues: variables,133 operationName: operationName,134 })120- } catch (error: unknown) {121- // Return 400: Bad Request if any execution context errors exist.122- c.res = c.json(errorMessages(['GraphQL execution context error.']), 400)135+ } catch (contextError: unknown) {136+ if (contextError instanceof Error) {137+ const e = new GraphQLError(contextError.message, {138+ originalError: contextError,139+ nodes: documentAST,140+ })141+ // Return 400: Bad Request if any execution context errors exist.142+ c.res = c.json(errorMessages(['GraphQL execution context error.'], [e]), 400)143+ }144 return145 }146 147 if (result.data == null) {127- c.res = c.json(errorMessages([result.errors.toString()]), 500)148+ c.res = c.json(errorMessages([result.errors.toString()], result.errors), 500)149 return150 }151 152 /*63 unmodified lines216 217 return params218 }219 199-export const errorMessages = (messages: string[]) => {200- const errors = messages.map((message) => {220+export const errorMessages = (221+ messages: string[],222+ graphqlErrors?: readonly GraphQLError[] | readonly GraphQLFormattedError[]223+) => {224+ if (graphqlErrors) {225 return {202- message: message,226+ errors: graphqlErrors,227 }204- })228+ }229+230 return {206- errors: errors,231+ errors: messages.map((message) => {232+ return {233+ message: message,234+ }235+ }),236 }237 }238 239 export const graphiQLResponse = () => {}