From 0c32c1b092e2ceb65e4b39c5212ff2fb0308d513 Mon Sep 17 00:00:00 2001 From: Emile Fugulin Date: Tue, 10 Nov 2020 12:30:54 -0500 Subject: [PATCH] fix(plugin): Call the default error mapper function if present --- src/config.ts | 1 - src/plugin.ts | 6 +++++- tests/fixtures/server.ts | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/config.ts b/src/config.ts index 5dfb9aad..f20cddc0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,7 +5,6 @@ import { ShieldContext, ShieldRule } from './rules'; type HashFunction = (arg: { root: any; args: any }) => string; type ErrorMapper = ( - err: Error, parent: any, args: any, ctx: ShieldContext, diff --git a/src/plugin.ts b/src/plugin.ts index 141c959a..8dc77313 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -92,7 +92,11 @@ export const nexusShield = (settings: ShieldPluginSettings) => { : true; if (!allowed) { - throw options.defaultError; + let error = options.defaultError; + if (typeof error === 'function') { + error = await error(root, args, ctx, info); + } + throw error; } // Resolver diff --git a/tests/fixtures/server.ts b/tests/fixtures/server.ts index dbe4e49b..45f5f6fa 100644 --- a/tests/fixtures/server.ts +++ b/tests/fixtures/server.ts @@ -28,7 +28,7 @@ const schema = makeSchema({ outputs: false, plugins: [ nexusShield({ - defaultError: new ForbiddenError('DEFAULT'), + defaultError: () => new ForbiddenError('DEFAULT'), defaultRule: allow, }), ],