Skip to content

Commit

Permalink
fix(plugin): Call the default error mapper function if present
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Nov 10, 2020
1 parent 7c27851 commit 0c32c1b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const schema = makeSchema({
outputs: false,
plugins: [
nexusShield({
defaultError: new ForbiddenError('DEFAULT'),
defaultError: () => new ForbiddenError('DEFAULT'),
defaultRule: allow,
}),
],
Expand Down

0 comments on commit 0c32c1b

Please sign in to comment.