#Help throwing errors in filter hooks for frontend

6 messages · Page 1 of 1 (latest)

obsidian dome
#

I'm trying to create a filter hook that needs to check a user's balance before allowing a mutation to happen. I'm using something like the following:

const testError = createError('test', 'test', 400);

if(!user.balance) {
  throw new testError();
}

The directus console logs show this:

[13:55:05.089] ERROR: test
    err: {
      "type": "GraphQLError",
      "message": "test",
      "stack":
          DirectusError: test
              ...
      "path": [
        "create_picks_item"
      ],
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "extensions": {
        "code": "TEST"
      }
    }

Which looks like the error is being hit properly. But the front API call just shows this in the network response:

{
  "data": {
    "create_picks_item": null
  },
  "errors": [
    {
      "message": "An unexpected error occurred.",
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR"
      }
    }
  ]
}

Is there a way that I can have my custom test error sent in the response instead of the generic INTERNAL_SERVER_ERROR that it's giving now?

torn gyroBOT
#

Thanks for posting! This is a community powered server, so you may or may not get an answer based on available help and expertise. To increase your chances of somebody being able to help you, please help us help you making sure you:

  • Adding an explanation of exactly what you're trying to achieve.
  • Adding any and all related code or previous attempts.
  • Describing the exact issue or error you are facing.
  • Posting any screenshots if applicable.
  • Reading through https://stackoverflow.com/help/how-to-ask.

When you're done with this thread, please close it. Thanks! ✨

(If you have a support agreement and need help, please contact the core team via email.)

hearty ether
#

For filters and endpoints you can use the package @directus/validation which exports an error class named FailedValidationError. It's the same error used by directus builtin filters

#

In your case you would do:

import { FailedValidationError } from '@directus/validation';

if(!user.balance) {
  throw new FailedValidationError({
    field: 'balance',
    type: 'gt',
    valid: 0
  });
}
obsidian dome
#

thanks @hearty ether , i'll look into that and give it a shot