From 3c5bef33923ae32fc2adbe177b024668959618ba Mon Sep 17 00:00:00 2001 From: sooxt98 Date: Thu, 6 Jan 2022 13:39:50 +0800 Subject: [PATCH 1/3] add ErrorWithProps handler for mercurius hook --- lib/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/errors.js b/lib/errors.js index 4888ccc5..b7888a0b 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -43,7 +43,7 @@ function toGraphQLError (err) { } function defaultErrorFormatter (err, ctx) { - let errors = [{ message: err.message }] + let errors = [err instanceof ErrorWithProps ? formatError(toGraphQLError(err)) : { message: err.message }] // There is always app if there is a context const log = ctx.reply ? ctx.reply.log : ctx.app.log From a373a44c84ab3ed17e99b9393f95b21ae7757c61 Mon Sep 17 00:00:00 2001 From: sooxt98 Date: Thu, 6 Jan 2022 20:45:10 +0800 Subject: [PATCH 2/3] add ErrorWithProps preParsing test --- test/hooks.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/hooks.js b/test/hooks.js index 7b674d1c..f802c6d8 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -7,6 +7,7 @@ const { mapSchema } = require('@graphql-tools/utils') const { parse, buildSchema, GraphQLSchema } = require('graphql') const { promisify } = require('util') const GQL = require('..') +const { ErrorWithProps } = GQL const immediate = promisify(setImmediate) @@ -281,6 +282,53 @@ test('preParsing hooks should handle errors', async t => { }) }) +test('preParsing hooks should handle ErrorWithProps', async t => { + t.plan(4) + const app = await createTestServer(t) + + app.graphql.addHook('preParsing', async (schema, source, context) => { + t.type(schema, GraphQLSchema) + t.equal(source, query) + t.type(context, 'object') + throw new ErrorWithProps('a preParsing error occured', {code: 'USER_ID_INVALID'}) + }) + + app.graphql.addHook('preParsing', async (schema, source, context) => { + t.fail('this should not be called') + }) + + app.graphql.addHook('preValidation', async (schema, document, context) => { + t.fail('this should not be called') + }) + + app.graphql.addHook('preExecution', async (schema, document, context) => { + t.fail('this should not be called') + }) + + app.graphql.addHook('onResolution', async (execution, context) => { + t.fail('this should not be called') + }) + + const res = await app.inject({ + method: 'POST', + headers: { 'content-type': 'application/json' }, + url: '/graphql', + body: JSON.stringify({ query }) + }) + + t.same(JSON.parse(res.body), { + data: null, + errors: [ + { + message: 'a preParsing error occured', + extensions: { + code: 'USER_ID_INVALID' + } + } + ] + }) +}) + test('preParsing hooks should be able to put values onto the context', async t => { t.plan(8) const app = await createTestServer(t) From 861962cb2e3d2dc6c12881f5a268d5ecafc494e9 Mon Sep 17 00:00:00 2001 From: sooxt98 Date: Thu, 6 Jan 2022 22:47:44 +0800 Subject: [PATCH 3/3] Fix synxtax format --- test/hooks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hooks.js b/test/hooks.js index f802c6d8..f71e1b76 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -290,7 +290,7 @@ test('preParsing hooks should handle ErrorWithProps', async t => { t.type(schema, GraphQLSchema) t.equal(source, query) t.type(context, 'object') - throw new ErrorWithProps('a preParsing error occured', {code: 'USER_ID_INVALID'}) + throw new ErrorWithProps('a preParsing error occured', { code: 'USER_ID_INVALID' }) }) app.graphql.addHook('preParsing', async (schema, source, context) => {