Skip to content

Custom errors format #51

@hemache

Description

@hemache

Hello guys,

I've following custom error class

class TypedError extends Error {
    constructor(message: string, public code: number) {
        super(message)

        // Set the prototype explicitly.
        Object.setPrototypeOf(this, TypedError.prototype)
    }
}

which I use in controller like

@Mutation()
raiseAnError() {
    throw new TypedError('Not found', 404)
}

however, using the playground I only get following with no Error.code

"errors": [
    {
      "message": "Not found",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "raiseAnError"
      ]
    }
  ]

How can I achieve something like (https://codeburst.io/custom-errors-and-error-reporting-in-graphql-bbd398272aeb)

formatError(err) {
    errors.report(err, req);   // <-- log the error
    return {
      message: err.message,
      code: err.originalError && err.originalError.code,   // <--
      locations: err.locations,
      path: err.path
    };
  }```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions