Skip to content
Myr

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

2 files+43 added−15 removed

modified:src/middleware/graphql-server/graphql-server.test.ts-1 removed
544 unmodified lines
545 query: 'query helloWho($who: String){ test(who: $who) }',546 variables: { who: ['John', 'Jane'] },547 }),548 })549549 expect(res.status).toBe(500)550 })551552 it('Handles unsupported HTTP methods', async () => {
11 unmodified lines
modified:src/middleware/graphql-server/graphql-server.ts+43 added-14 removed
10 unmodified lines
11 validateSchema,12 validate,13 specifiedRules,14 getOperationAST,15 GraphQLError,16} from 'graphql'1717import type { GraphQLSchema, DocumentNode, ValidationRule, FormattedExecutionResult } from 'graphql'18import type {19 GraphQLSchema,20 DocumentNode,21 ValidationRule,22 FormattedExecutionResult,23 GraphQLFormattedError,24} from 'graphql'2526type Options = {27 schema: GraphQLSchema28 rootValue?: unknown
24 unmodified lines
53 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 lines
6869 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 }7879 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 }9293 // Validate AST, reporting any errors.94 const validationErrors = validate(schema, documentAST, [...specifiedRules, ...validationRules])9596 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 }101102 if (c.req.method === 'GET') {
28 unmodified lines
131 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 }146147 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 }151152 /*
63 unmodified lines
216217 return params218}219199export const errorMessages = (messages: string[]) => {200 const errors = messages.map((message) => {220export 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 }229230 return {206 errors: errors,231 errors: messages.map((message) => {232 return {233 message: message,234 }235 }),236 }237}238239export const graphiQLResponse = () => {}