Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom errors format #51

Open
hemache opened this issue Mar 20, 2019 · 0 comments
Open

Custom errors format #51

hemache opened this issue Mar 20, 2019 · 0 comments

Comments

@hemache
Copy link

hemache commented Mar 20, 2019

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
    };
  }```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant