Apollo-server: Error properties not available within formatError

Created on 19 Oct 2016  路  2Comments  路  Source: apollographql/apollo-server

import ExtendableError from 'es6-error'

export default class MyError extends ExtendableError {
  constructor(args) {
    let code = Object.keys(args)[0]
    let message = Object.values(args)[0]
    super(message)
    this.code = code
  }
}

I have a simple error handler works something like this:

let INVALIDREQUEST = 'invalid request'
let e = new MyError({INVALIDREQUEST})
console.log(e.code) // => "INVALIDREQUEST"

I'm having trouble because when I log error.code from within formatError it's not available.

formatError: function (error) {
  console.log(error.code) // => undefined
  return error
}

How can I propagate custom properties (like code) of error from within formatError?

Also posted to stackoverflow

Most helpful comment

All 2 comments

Answering this on SO.

Was this page helpful?
0 / 5 - 0 ratings