mercurius icon indicating copy to clipboard operation
mercurius copied to clipboard

Gateway Service HTTP Errors

Open j opened this issue 3 years ago • 7 comments

It looks like the Mercurius Gateway service expects "errors" to exist in the response, however, if I have a fastify plugin that throws an error before it ever hits GraphQL, then the gateway emits an error.

{
  "errors": [
    {
      "message": "Cannot read property 'profile' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "profile"
      ]
    }
  ],
  "data": null
}

It might be nice if Mercurius handled common HTTP errors (Unauthorized, Forbidden, etc) so that it just emits that error on those types of exceptions, or if each gateway service could configure it's response to format the error if it exists.

j avatar Jul 28 '21 19:07 j

Sure thing! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jul 28 '21 20:07 mcollina

@j I guess these http errors can be handled using a custom errorFormatter (https://mercurius.dev/#/docs/api/options?id=error-formatter). Have you tried it?

sameer-coder avatar Jan 12 '22 13:01 sameer-coder

@mcollina Do you think we should we should change the error implementation in mercurius to accommodate these http errors or leave it for now since the same can be achieved using the errorFormatter?

sameer-coder avatar Jan 19 '22 06:01 sameer-coder

I think some minimal change is needed, the described error is not a good DX. At a minimum, we should avoid that: make sure that we support the errors property to not be there.

I'm unsure we should add specific handling of http status codes.

mcollina avatar Jan 19 '22 07:01 mcollina

@mcollina Do you mean we should remove the errors property if the error is thrown by a fastify-plugin before the request reaches GraphQL?

sameer-coder avatar Jan 27 '22 14:01 sameer-coder

Let me close this issue as I do not have much time to scope it.

mcollina avatar Jan 27 '22 15:01 mcollina

@j can you please provide the code snippet that reproduces the issue? especially the part that triggers this particular error.

sameer-coder avatar Jan 31 '22 11:01 sameer-coder