Error types (instanceof) are lost if thrown from resolver
See original GitHub issueapollo-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:
- Created 4 years ago
- Reactions:4
- Comments:12 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Have you tried checking
error.originalError
?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.