Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Allow handler error with an express middleware #443

Closed

Conversation

adriexnet
Copy link

Allow handler error with an express middleware

What does this PR do?

Allow using an express middleware as handler error

How should this be tested?

• Configure graphqlHTTP with handleErrorWithExpress

app.use('/graph', graphqlHTTP(async (req) => {
  return {
    graphiql: true,
    schema,
    handleErrorWithExpress: true,
    formatError(error) {
      logger.error(error.message, {
        path: error.path,
        locations: error.locations,
        stack: error.stack ? error.stack.split('\n') : [],
        trace: error.trace,
      });
      return {
        message: error.message,
        status: 400,
      };
    }
  };
}));

• Add an error middleware

app.use((error, req, res, next) => {
  if (res.headersSent) {
    return next(error);
  }

  return res.status(error.status || 500).send({
    error: error.message
  });
});

@mitodrummer
Copy link

Any idea when this will be merged and released to npm?

@ValfarDeveloper
Copy link

Oh man this is so useful! It's a shame that yet is not implemented.

result.errors &&
result.errors.length
) {
return nextFunction(result.errors[0]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we should return all the errors in the array, not just the first one.

@augustohsalla
Copy link

for god sake, accept this!

@driescroons
Copy link

@aagtt @jsonmaur

I wouldn't add the ability to add error middleware to the package itself. I've handled it like so:

Here a demo:
https://streamable.com/hvtjg3

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants