'Cannot log stack traces with apollo server

I'm building a graphql application with apollo server and I'm having trouble figuring out how to log stack traces serverside in production mode.

I found these docs but it's not clear how to correctly log stack traces in production.

I have the following formatError function:

    formatError: ((err) => {
      console.error(err.originalError)
      return err
  })

and when one of my resolvers throws an error with NODE_ENV=production I see the following output:

Error: Unexpected error value: "could not get config value"
    at locatedError (/tripvector/node_modules/graphql/error/locatedError.js:24:9)
    at /tripvector/node_modules/graphql/execution/execute.js:491:54
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async execute (/tripvector/node_modules/apollo-server-core/dist/requestPipeline.js:204:20)
    at async processGraphQLRequest (/tripvector/node_modules/apollo-server-core/dist/requestPipeline.js:138:28)
    at async processHTTPRequest (/tripvector/node_modules/apollo-server-core/dist/runHttpQuery.js:187:30)

However, when I run in debug mode, the original error is printed as I expect:

Error: could not get config value
    at validate (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/lib/settings/settings.js:11:15)
    at getGoogleSecret (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/lib/settings/settings.js:29:12)
    at Object.loginWithGoogle (file:///Users/paymahn/code/tripvector/tripvector-mono/backend/api/users/graphql/mutations.js:141:30)
    at field.resolve (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/apollo-server-core/dist/utils/schemaInstrumentation.js:52:26)
    at executeField (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:469:20)
    at /Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:365:22
    at promiseReduce (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/jsutils/promiseReduce.js:23:9)
    at executeFieldsSerially (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:361:43)
    at executeOperation (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:335:14)
    at execute (/Users/paymahn/code/tripvector/tripvector-mono/backend/node_modules/graphql/execution/execute.js:130:20)

I throw the error with the following code:

function validate(val) {
    if (!val) {
        throw new Error('could not get config value')
    }
    return val
}

How can I correctly configure apollo server to print stack traces for errors, even in production?



Solution 1:[1]

As usual, I found the answer 2 minutes after asking. I was doing some silly error catching and rethrowing in promises which is why this problem was happening only in production.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paymahn Moghadasian