Apollo-server: Apollo Server returns INTERNAL_SERVER_ERROR as code after throwing AuthenticationError

Created on 27 May 2018  Â·  14Comments  Â·  Source: apollographql/apollo-server

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"
              ]
            }
          ]
        }
      }
    }
  ]
}
has-reproduction 📚 good-first-issue

Most helpful comment

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

All 14 comments

@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!

I'm throwing the error in the resolver

On Wed 30 May, 2018, 10:13 AM Evans Hauser, notifications@github.com
wrote:

@harisvsulaiman https://github.com/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
https://github.com/apollographql/apollo-server/pull/1063/files#diff-4793189b9a83e7b56e97bd024540b79fR332,
I would love to help solve the issue!

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/apollo-server/issues/1100#issuecomment-393028567,
or mute the thread
https://github.com/notifications/unsubscribe-auth/Ab-HrUYvIuWsm4btTPMNqlusGi337kT9ks5t3iNUgaJpZM4UPMY5
.

>

Harisvsulaiman.

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?

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

Solved with #1113

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

@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.

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

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

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

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.

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",

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
}

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
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

danilobuerger picture danilobuerger  Â·  3Comments

stevezau picture stevezau  Â·  3Comments

veeramarni picture veeramarni  Â·  3Comments

Magneticmagnum picture Magneticmagnum  Â·  3Comments

disyam picture disyam  Â·  3Comments