Graphql: GraphQL error handling is incompatible with HttpException and Exception Filters

Created on 11 Feb 2018  路  10Comments  路  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


Using Nestjs with the GraphQLModule as documentation describes, there are a problem with throwing HttpException. The error message that GraphQL returns, contains "[Object Object"] in the message field instead the HttpException message.
The GraphQL.js library is expecting an Error instance, but HttpException not inherit from Error. What is the main reason for HttpException is not extending from Error?

In addition to this any Exception Filter is not working.

Expected behavior


Proper error handling and Exception Filters working with GraphQL.

Minimal reproduction of the problem with instructions


Install @nestjs/graphql and configure it as documentation describes. In any resolver try to throw a HttpException (or a inherited custom one). GraphQL returns an error like this:

{
  "data": {
    "findOneUserById": null
  },
  "errors": [
    {
      "message": "[object Object]",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "findOneUserById"
      ]
    }
  ]
}

What is the motivation / use case for changing the behavior?


Proper error handling working with GraphQL and documentation for how to deal with this.

Environment


- "@nestjs/common": "^4.5.9",
-  "@nestjs/core": "^4.5.10",
-  "@nestjs/graphql": "^2.0.0",
-  "@nestjs/microservices": "^4.5.8",
-  "@nestjs/testing": "^4.5.5",
-  "@nestjs/websockets": "^4.5.8",


For Tooling issues:
- Node version: 9.4.0  
- Platform:  Linux 

Others:

- Kubuntu
- WebStorm
- GraphiQL
- npm

Most helpful comment

I'm still encountering this issue with the current release! (5.0.0)
Any news how to fix that?

All 10 comments

I think the simplest "solution" would be to override the toString method to something like

class HttpException extends Error {
  // ...
  toString() {
    return JSON.stringify(this);
  }
}

this would result in {"mesage":"error...","code":200} instead of [object Object]
@kamilmysliwiec let me know if this seems like a good solution, I can submit a pr

Do you still encounter this issue with the latest release? (4.6.6)

I'm still encountering this issue with the current release! (5.0.0)
Any news how to fix that?

Any Solution?

same here 馃

also struggling with this (version 6.6.7)

Any Solution? @kamilmysliwiec

Still an issue on 6.5.3. Maybe there is some suggested workaround to make it work with e.g. apollo-link-error? The message field should contain the thrown error message used on the server and communicate that string to the client (instead of the [object Object] response).

bump

This is still an issue, especially when using the Federated GraphQL

Was this page helpful?
0 / 5 - 0 ratings