graphql / express-graphql

Create a GraphQL HTTP server with Express.
MIT License
6.34k stars 538 forks source link

Difficulty returning custom errors #732

Closed lingo closed 3 years ago

lingo commented 3 years ago

I may be missing something, but as far as I can see, the only way to really customize the error returned from resolver functions is via the customFormatErrorFn option. The headers, status code, and graphqlErrors members of errors returned from resolver functions are ignored. The only member of the thrown error that is used is the result of toString.

https://github.com/graphql/express-graphql/blob/43ba6061388b9a1fc0119dc8e909c7a4391c70e6/src/index.ts#L371

On this line, error is set up, and from this point on the incoming rawError is not referenced. This means that many of the if statements following are pointless since the condition will always return the same result.

for example:

   ...
   response.statusCode = error.status; // always 500
   const { headers } = error; // always comes from `httpError`
   if (headers != null) {
        for (const [key, value] of Object.entries(headers)) {
          response.setHeader(key, String(value));
        }
    }

   if (error.graphqlErrors == null) // always null
   ...
lingo commented 3 years ago

Nevermind, can't read code today! I see that createError is actually taking the rawError as a parameter.