Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

The error formatter is dropping custom fields #444

Closed
justinmchase opened this issue Jun 26, 2018 · 4 comments
Closed

The error formatter is dropping custom fields #444

justinmchase opened this issue Jun 26, 2018 · 4 comments

Comments

@justinmchase
Copy link

justinmchase commented Jun 26, 2018

I am throwing an error in my handler but later in the formatError function the error object is of a different type and it has dropped all of my custom fields.

throw Object.assign(new Error('example'), { code: 'E_EXAMPLE' })
  app.use('/api', graphql({
    schema,
    rootValue: root
    graphiql: true,
    formatError: error => console.log(error) || ({
      message: error.message,
      code: error.code, // null, why?
      locations: error.locations,
      stack: error.stack ? error.stack.split('\n') : [],
      path: error.path
    })
  }))

How can I throw an error which has fields that persist into the error formatter?

@IvanGoncharov
Copy link
Member

@justinmchase It should be either:

throw Object.assign(new Error('example'), { extensions: {code: 'E_EXAMPLE' }})

Or you should use error.originalError.code inside formatError.

@justinmchase
Copy link
Author

justinmchase commented Jun 26, 2018

Perfect, both worked for me, thanks!

For the record in both cases extensions and originalError are propagated sub properties on the error. Such as:

throw Object.assign(new Error('example'), { A; 'a', extensions: { B: 'b' } })

// ...
formatError: ({ originalError: { A }, extensions: { B } }) => ({
  A,
  B
})

@IvanGoncharov
Copy link
Member

IvanGoncharov commented Jun 26, 2018

@justinmchase Note that latest version of GraphQL spec changed things a bit:
https://github.com/facebook/graphql/releases/tag/June2018

Previously GraphQL did not make it clear how services should add additional data to errors. After #230, there was a concern that adding new features to errors could accidentally conflict with this additional data. Now, any additional data on an error should be contained within an extensions field.

In graphql-js v14.0.0(RC stage at the moment) extensions from original error would be formatted as error.extensions.code:
graphql/graphql-js#1284

@Enado95
Copy link

Enado95 commented Jul 13, 2020

originalError

Works for me. Here's how i did for those who are wondering. I created my own custom error:

'use strict'

module.exports = function CustomError(message, extra) {
  Error.captureStackTrace(this, this.constructor)
  this.name = this.constructor.name
  this.message = message
  this.extra = extra
}

require('util').inherits(module.exports, Error)

Threw the error in the resolver and then referenced my additional property like so:

app.use(
  '/graphql',
  graphqlHTTP(async (request, response, graphQLParams) => {
    return {
      schema,
      graphiql: process.env.NODE_ENV === 'development' ? true : false,
      context: {
        req: request
      },
      customFormatErrorFn: (error) => ({
        status: error.originalError.extra,
        message: error.message,
        locations: error.locations,
        stack: error.stack ? error.stack.split('\n') : [],
        path: error.path
      })
    }
  })
)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants