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

Apollo Server returns INTERNAL_SERVER_ERROR as code after throwing AuthenticationError #1100

Closed
harisvsulaiman opened this issue May 27, 2018 · 14 comments
Labels
📚 good-first-issue Issues that are more approachable for first-time contributors. 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository.

Comments

@harisvsulaiman
Copy link

Apollo Server @2.0beta returns INTERNAL_SERVER_ERROR as code after throwing AuthenticationError.
As per the docs it should have thrown UNAuthenticated as code in extentions

code
throw AuthenticationError("Please Signin");

package.json

"dependencies": {
    "apollo-engine": "^1.1.1",
    "apollo-link-context": "^1.0.8",
    "apollo-link-http": "^1.5.4",
    "apollo-server": "^2.0.0-beta.2",
    "apollo-server-express": "^2.0.0-beta.1",
    "body-parser": "^1.18.2",
    "express": "^4.16.3",
    "firebase-admin": "~5.12.0",
    "graphql": "^0.13.2",
    "graphql-import": "^0.5.2",
    "graphql-tools": "^3.0.0",
    "node-fetch": "^2.1.2"
  },

error

{
  "data": null,
  "errors": [
    {
      "message": "Please Signin",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "errors": [
            {
              "message": "Please Signin",
              "locations": [],
              "path": [
                "user"
              ]
            }
          ]
        }
      }
    }
  ]
}
@ghost ghost added ✋ blocking Prevents production or dev due to perf, bug, build error, etc.. 📚 good-first-issue Issues that are more approachable for first-time contributors. 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository. labels May 27, 2018
@evans
Copy link
Contributor

evans commented May 30, 2018

@harisvsulaiman Thanks for opening the issue!

Where are you throwing the AuthenticationError? Does it happen in resolvers or are you throwing it in the context function?

If you have a chance to create a test, in the style of this example, I would love to help solve the issue!

@harisvsulaiman
Copy link
Author

harisvsulaiman commented May 30, 2018 via email

@evans
Copy link
Contributor

evans commented May 30, 2018

Ah okay, are you running with NODE_ENV set to production? I've got a hunch that the error is getting masked in that case.

The other option could be that when the error is propagated up because the non-nullability it changes how the error is handled. Can you post the section of your schema and what your resolvers look like?

@evans
Copy link
Contributor

evans commented May 31, 2018

@harisvsulaiman If you have a stacktrace for the error, that would be extremely helpful as well!

@evans
Copy link
Contributor

evans commented Jun 13, 2018

Solved with #1113

@evans evans closed this as completed Jun 13, 2018
@danferreira
Copy link
Contributor

danferreira commented Jun 18, 2018

Hey @evans. I'm still getting the same generic error. Should I open a new issue?
This:

Query: {
            contacts: (root, args, { models }) => {
            throw new AuthenticationError('must authenticate');
        },
    },

results in:

{
  "data": {
    "contacts": null
  },
  "errors": [
    {
      "message": "must authenticate",
      "locations": [],
      "path": [
        "contacts"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "errors": [
            {
              "message": "must authenticate",
              "locations": [],
              "path": [
                "contacts"
              ]
            }
          ],
          "stacktrace": [
            "Error: must authenticate",
            "    at new CombinedError (/app/node_modules/graphql-tools/src/stitching/errors.ts:85:5)",
            "    at Object.checkResultAndHandleErrors (/app/node_modules/graphql-tools/src/stitching/errors.ts:107:11)",
            "    at CheckResultAndHandleErrors.transformResult (/app/node_modules/graphql-tools/src/transforms/CheckResultAndHandleErrors.ts:15:12)",
            "    at /app/node_modules/graphql-tools/src/transforms/transforms.ts:37:45",
            "    at Array.reduce (<anonymous>)",
            "    at applyResultTransforms (/app/node_modules/graphql-tools/src/transforms/transforms.ts:35:21)",
            "    at /app/node_modules/graphql-tools/src/stitching/delegateToSchema.ts:81:12",
            "    at step (/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:32:23)",
            "    at Object.next (/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:13:53)",
            "    at fulfilled (/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:4:58)",
            "    at <anonymous>",
            "    at process._tickCallback (internal/process/next_tick.js:182:7)"
          ]
        }
      }
    }
  ]
}

I'm using:

"apollo-server": "^2.0.0-rc.0",
"apollo-server-express": "^2.0.0-rc.0",

EDIT: nevermind, I was using the old docs as reference. Now I see what was my error

@harisvsulaiman
Copy link
Author

@evans I apologize for the delay!
BTW i implemented generic Apollo Error so that i could continue development.I'm sticking to the strategy for now.

@chentsulin
Copy link
Contributor

chentsulin commented Jul 17, 2018

It seems that there are some issues between schema stitching and apollo errors:

@todkap
Copy link

todkap commented Oct 18, 2018

Sorry for cross-linking but found this github issue after I posted my comment. I think we are seeing this issue as well (ardatan/graphql-tools#480 (comment)) when we throw a ApolloError or AuthenticationError from our resolver.

@richardscarrott
Copy link

@todkap I'm also having the same problem using apollo-server-express v2.1.0 -- did you find a solution?

@todkap
Copy link

todkap commented Nov 7, 2018

Our issue was on the graphql-tools version, rolled back from 4.0.1 to 3.1.1

Rest package are:
  graphql: ^0.13.2
  apollo-server: ^2.1.0
  graphql-tools: ^3.1.1

Also need to apply formatError word-around that suggested by other posts in order to get this to work.

@kfrajtak
Copy link

kfrajtak commented Jun 26, 2019

I am also experiencing this error - I have these packages installed and I am using graphql-modules, which means schema stitching.

"apollo-server": "^2.6.7",
"apollo-server-core": "^2.6.7",
 "apollo-server-express": "^2.6.7",
"graphql": "^14.4.0",

@abernix abernix removed the ✋ blocking Prevents production or dev due to perf, bug, build error, etc.. label Sep 3, 2019
@nicolaswehmeyer
Copy link

Currently having the same issue adding an authentication middleware to my API. This is my middleware right now (unfinished), it get's called whenever a user requests information from a secured API and I always receive this error:

"errors": [
    {
      "message": "AuthenticationError is not defined",
...
export const isAuthenticated = async (resolve, root, args, context, info) => {
  if(!context.authenticated) {
    throw new AuthenticationError('You must be logged in to request information form this API')
  }
  const result = await resolve(root, args, context, info)
  return result
}

@nicolaswehmeyer
Copy link

nicolaswehmeyer commented Aug 21, 2020

Currently having the same issue adding an authentication middleware to my API. This is my middleware right now (unfinished), it get's called whenever a user requests information from a secured API and I always receive this error:

"errors": [
    {
      "message": "AuthenticationError is not defined",
...
export const isAuthenticated = async (resolve, root, args, context, info) => {
  if(!context.authenticated) {
    throw new AuthenticationError('You must be logged in to request information form this API')
  }
  const result = await resolve(root, args, context, info)
  return result
}

UPDATE
In case it helps anyone, I found the issue quickly after my comment. I needed to import apollo-server-express first, as AuthenticationError gets provided by it. Here is the new code that is working fine (I used Error first instead of AuthenticationError):

import Server from 'apollo-server-express'

export const isAuthenticated = async (resolve, root, args, context, info) => {
  if(!context.authenticated) {
    throw new Server.AuthenticationError('You must be logged in to request information form this API')
  }
  const result = await resolve(root, args, context, info)
  return result
}

@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
📚 good-first-issue Issues that are more approachable for first-time contributors. 🌹 has-reproduction ❤ Has a reproduction in Glitch, CodeSandbox or Git repository.
Projects
None yet
Development

No branches or pull requests

9 participants