question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom errors for rules and global fallback

See original GitHub issue

Hi @maticzav! First of all thanks for the awesome library!

I have few questions/suggestions about CustomErrors:

  1. Is there a way to specify CustomError that will be returned from shield in case if permissions checks are failed?

Example: I have isAuthenticated rule

export default rule()((parent, args, { user }) => Boolean(user));

I want to return my custom 401 error like this

import { SevenBoom } from 'graphql-apollo-errors';

rule(null, { 
  customError: SevenBoom.unauthorized('You are not authenticated')
})((parent, args, { user }) => Boolean(user))
  1. Is there a way to specify global CustomError instead of https://github.com/maticzav/graphql-shield/blob/master/src/index.ts#L234 which will be used in case if there is no rule specific error?

Example:

import { SevenBoom } from 'graphql-apollo-errors';

shield({ /* Rules… */ }, {
  customError: SevenBoom.forbidden('You are not allowed to do this action.')
})

I can implement this and looks like it is not a breaking change.

Just want to clarify if there is no way to do this in current version of the library?

Let me know if you have any questions. Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
blazestudios23commented, Jul 14, 2021

Is there no way to just map error messages to rules? That seems like the way to go.

1reaction
reergymerejcommented, Jun 4, 2021

I’m a few years late, but this seems to be a general problem with functions doing more than one thing. Rules should just test and return a boolean. That’s a separate concern from deciding which error to throw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom errors for rules and global fallback · Issue #60 - GitHub
To conclude, I propose to change the functionality by adding customError or error function which is returned from the rule in case of...
Read more >
Errors – GraphQL Shield
Custom Errors. Shield, by default, catches all errors thrown during resolver execution. This way we can be 100% sure none of your internal...
Read more >
Error Messages - VeeValidate
By default, any unspecified rules for the specific field messages will fallback to the already included ones, so you only need to define...
Read more >
Create Custom Error Response Rules | Imperva
A custom error response rule enables you to replace the default error response and error code that are returned to the client.
Read more >
Handling Error Conditions Using a Step Functions State ...
Learn how to handle error conditions using an AWS Step Functions state machine. ... The context object returns the error message This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found