Skip to content
Snippets Groups Projects
Commit 358d8faf authored by Giannis Kopanas's avatar Giannis Kopanas
Browse files

fix(server): add error handling for graphql errors

parent 3cbc97bf
No related branches found
No related tags found
1 merge request!9fix(server): add error handling for graphql errors
const { ApolloServer } = require('apollo-server-express') const { ApolloServer, ForbiddenError, UserInputError, AuthenticationError, ApolloError } = require('apollo-server-express')
const isEmpty = require('lodash/isEmpty') const isEmpty = require('lodash/isEmpty')
const config = require('config') const config = require('config')
...@@ -44,13 +44,19 @@ const api = app => { ...@@ -44,13 +44,19 @@ const api = app => {
pubsweetError => error instanceof pubsweetError, pubsweetError => error instanceof pubsweetError,
) )
const isGraphqlDefinedError = [ForbiddenError, UserInputError, AuthenticationError, ApolloError].some(
graphqlError => error instanceof graphqlError
)
// err is always a GraphQLError which should be passed to the client // err is always a GraphQLError which should be passed to the client
if (!isEmpty(err.originalError) && !isPubsweetDefinedError) if (!isEmpty(err.originalError) && !isPubsweetDefinedError && !isGraphqlDefinedError)
return { return {
name: 'Server Error', name: 'Server Error',
message: 'Something went wrong! Please contact your administrator', message: 'Something went wrong! Please contact your administrator',
} }
if (isGraphqlDefinedError) return error
return { return {
name: error.name || 'GraphQLError', name: error.name || 'GraphQLError',
message: error.message, message: error.message,
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment