Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most of the predefined ApolloError subclasses fail to pass through extensions to ApolloError #5307

Closed
bristoljon opened this issue Jun 15, 2021 · 1 comment

Comments

@bristoljon
Copy link
Contributor

"apollo-server": "^2.18.2",
"apollo-server-express": "^2.24.0",

Throwing a ForbiddenError with extra fields as described here does not work and the errors array received by the client contains an extensions field with only the standard Apollo fields.

  Query: {
    hello: (root, args, context) => {
      throw new ForbiddenError("Goodbye cruel world!", {
        extra: "data",
        and: "other custom fields"
      });
    }
  }

Results in:

{
  "errors": [
    {
      "message": "Goodbye cruel world!",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "hello"
      ],
      "extensions": {
        "code": "FORBIDDEN",
        "exception": {
          "stacktrace": [...]
        }
      }
    }
  ],
  "data": {
    "hello": null
  }
}

However the extra fields do appear in the response if using a UserInputError but I would expect them to be consistent.

Perhaps this is by design but if that is the case it would be good to update the docs at url above. And we do have a use case where a ForbiddenError is the right choice semantically but we need to include extra data in the response as well.

Code sandbox: https://codesandbox.io/s/eloquent-mclean-f9v1l?file=/index.js:264-445

Thanks,

Jon

@glasser
Copy link
Member

glasser commented Jun 15, 2021

It looks like ForbiddenError's constructor doesn't pass through an extensions argument to its superclass like UserInputError does. Adding that (I'd say to all the ApolloError subclasses except the two persisted query ones which probably shouldn't be thrown from end-user code) sounds reasonable. Feel free to file a PR against the release-3.0 branch!

@glasser glasser changed the title ForbiddenError does not include extra custom fields Most of the predefined ApolloError subclasses fail to pass through extensions to ApolloError Jun 15, 2021
@glasser glasser closed this as completed Jun 15, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants