Graphql-js: Custom error property

Created on 8 Jan 2018  路  1Comment  路  Source: graphql/graphql-js

I am doing i18n translation after GraphQL done if it throw an error.

With the information of Error, I have code, prefix, In my dictionary, easy to translate it.

Unfortunately, the property are missing.

      type: Me,
      async resolve(root: any, params: any, req: any) {
        const err: any = new Error('Test error');
        err.code = 123; // custom error property
        if (err) {
          throw err;
        }
        return Me;
      }

Koa-graphql formatError

      graphqlHTTP({
        schema: Schema,
        graphiql: true,
        formatError(err) {
          console.log(err); // print it out
          return {
            message: err.message,
            path: err.path,
            locations: err.locations,
            stack: err.stack.split('\n'),
            code: err.code,
            prefix: err.prefix,
            detail: err.detail
          };
        }
      })

Here is the output in terminal

{ Error: Test error
  at resolve (/home/xxxx/app/graphql/mutation/index.ts:19:26)
  at resolveFieldValueOrError (/home/xxxx/node_modules/graphql/execution/execute.js:544:18)
  at resolveField (/home/xxxx/node_modules/graphql/execution/execute.js:508:16)
  at /home/xxxx/node_modules/graphql/execution/execute.js:330:20
  at <anonymous>
  at process._tickDomainCallback (internal/process/next_tick.js:228:7)
  message: 'Test error',
    locations: [ { line: 2, column: 3 } ],
  path: [ 'me' ] }

The custom property code have been drop

Is anyone can help me found it back?

Related

  • #295
  • #912
  • #928

Most helpful comment

Finally, I found the way.

      graphqlHTTP({
        schema: Schema,
        graphiql: true,
        formatError(err) {
          const originalError = err.originalError; // origin error
          console.log(err); // print it out
          return {
            message: err.message,
            path: err.path,
            locations: err.locations,
            stack: err.stack.split('\n'),
            code: err.code,
            prefix: err.prefix,
            detail: err.detail
          };
        }
      })

>All comments

Finally, I found the way.

      graphqlHTTP({
        schema: Schema,
        graphiql: true,
        formatError(err) {
          const originalError = err.originalError; // origin error
          console.log(err); // print it out
          return {
            message: err.message,
            path: err.path,
            locations: err.locations,
            stack: err.stack.split('\n'),
            code: err.code,
            prefix: err.prefix,
            detail: err.detail
          };
        }
      })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

adriano-di-giovanni picture adriano-di-giovanni  路  3Comments

scf4 picture scf4  路  3Comments

swist picture swist  路  4Comments

itajaja picture itajaja  路  3Comments

davide-ganito picture davide-ganito  路  4Comments