I'm pretty sure that the question is self explanatory. I'm using 'apollo-server-core' 3.6.5
Errors I want to get
{
"errors": [
{
"message": "Syntax Error: Unexpected <EOF>.",
"locations": [
{
"line": 1,
"column": 1
}
]
}
]
}
Errors I do get
{
"errors": [
{
"message": "GraphQL operations must contain a non-empty `query` or a `persistedQuery` extension.",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"GraphQLError: GraphQL operations must contain a non-empty `query` or a `persistedQuery` extension.",
" at processGraphQLRequest (C:\\Users\\XXXX\\Documents\\Github\\XXXX-XXX\\node_modules\\apollo-server-core\\src\\requestPipeline.ts:204:7)",
" at processTicksAndRejections (node:internal/process/task_queues:96:5)",
" at async processHTTPRequest (C:\\Users\\XXXX\\Documents\\Github\\XXXX-XXXX\\node_modules\\apollo-server-core\\src\\runHttpQuery.ts:346:24)"
]
}
}
}
]
}
My initial thought was parse the error into an object, and map out any undesirables like so
const msg = JSON.parse(error.message).errors.map((err:any) => err = {message: err.message});
But that dose not work as it dose not tell you where the mistake occurred + it seems unnecessary to use JSON.parse and then stringify'ing it right back.
The apollo docs cover error handling. https://www.apollographql.com/docs/apollo-server/data/errors/#throwing-errors
You can create a custom error like this:
import { ApolloError } from 'apollo-server-errors';
throw new ApolloError('My error message', 'MY_ERROR_CODE', myCustomExtensions);