Return custom errors with status code on GraphQL

Photo by Luca Bravo on Unsplash

After trying to return some status code on my error response using GraphQL, I found that it was not that easy as setting `res.status(xxx)` , so, was there that I decided to create an approach that can be useful.

Visit the repo on GitHub

In our App.js you need to add formatError that will handle all the error that you throw from your queries and mutations.

To create our own error response we need to do different things.
Create a constants file with the error name and the error type:

In your query or mutation you should require the constants file, and when you resolve it, in case that there is an error, you might throw the error with the constant that you require.

Also, we will need a function that will search on errorType for the properties of the error; we can create a new file or set it on App.js

And finally, we might update App.js so we can return our custom error

Now the error has the properties message and statusCode.