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.

Error types (instanceof) are lost if thrown from resolver

See original GitHub issue

apollo-server-express@2.6.3

If I throw an AuthenticationError from a resolver, I cannot detect this from the formatError handler. For example:

// somewhere in a resolver...
import { AuthenticationError } from 'apollo-server-express'
const resolve = (_, args, context) => {
  throw new AuthenticationError('test')
}

// in formatError handler
const formatError = (error) => {
  console.log(error instanceof AuthenticationError) // returns false
  return error
}

HOWEVER, if the error is SyntaxError or ValidationError produced by GraphQL automatically due to a malformatted graphql request, then the instanceof check works.

A quick working sample: https://codesandbox.io/s/apollo-server-ft3ul?fontsize=14

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
bnaokicommented, Jul 2, 2019

Have you tried checking error.originalError?

5reactions
kabachookcommented, Jun 21, 2019

Same for me, it just returns GraphQLError for everything. My version is 2.6.4. But I believe this bug also works since versions ^2.4.8.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors thrown in angular 6 resolver lose error custom type
When I throw the error in the resolver the instance type is changed to Error and instanceof DisplayableError returns false.
Read more >
Error handling - Apollo GraphQL Docs
For example, it throws a GRAPHQL_VALIDATION_FAILED error whenever an incoming operation isn't valid against the server's schema. Your resolvers can also throw ......
Read more >
Improving TypeScript error handling with exhaustive type ...
Discover an improved method for handling errors in TypeScript that solves problems that arise from returning null and throwing try...catch.
Read more >
TypeError: invalid 'instanceof' operand 'x' - JavaScript | MDN
The JavaScript exception "invalid 'instanceof' operand" occurs when the right-hand side operands of the instanceof operator isn't used with ...
Read more >
Better Error Handling In NodeJS With Error Classes
This approach enables us to distinguish the errors thrown by our application. So now if we want to handle a bad request error...
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