 

JavaScript・2 min read・Last update on November 25, 2020

# Handling GraphQL Errors with Status Code

 

 

 

 ![Handling GraphQL Errors with Status Code](/sites/default/files/remediation-media/64e5de4abe3aedf12fd60b9e_Handling_20errors_20in_20GraphQL_20with_20Status_20code.avif) 

 



 

   Table of contents  - [The question is - How do we handle errors in GraphQL? How can we do it in a way that’s easy to understand?](#the-question-is-how-do-we-handle-errors-in-graphql-how-can-we-do-it-in-a-way-that-s-easy-to-understand)
- [Let’s see how to handle errors with a status code](#let-s-see-how-to-handle-errors-with-a-status-code)
- [Conclusion](#conclusion)
 
  

 

Any application, from simple to complex, can have errors. It is important to handle these errors and report these errors back to your users for information.

### The question is - How do we handle errors in GraphQL? How can we do it in a way that’s easy to understand?

Let’s start by running a simple GraphQL query:

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/fef6610f1c943728d295c324.avif) Here we can see, we get an error, this error can be found in the errors array.

This is how errors can be found in a GraphQL response. But the error is not very clear and meaningful. Here are some problems with such error representations:

- All errors are treated the same
- It’s difficult to know where the error came from
- It’s tough for the user to understand what exactly the errors talk about

### Let’s see how to handle errors with a status code

For this, you need to add customFormatErrorFn that will handle all the errors thrown from your queries and mutations. Let’s see the steps for creating our error response.

#### How to do it!

1. Create a constants file with the error name and the error type: errorConstant.ts

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/8ea28b5d28e3e6bc3a1d4eb9.avif) 1. 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 in index.ts file

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/13a5b63f7be3c2b27005e68a.avif) 1. When you resolve a query or mutation, and an error occurs, you might throw the error message with the constant that you define in errorConstant.ts file.

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/ee09d6e668f7412b794bf91d.avif) 1. And finally, we update index.js so we can return our custom error. We‘ll add customFormatErrorFn in graphqlExpress when initializing GraphQL within Express.

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/63345de8b6053a5ec37a03bd.avif) #### Let’s see the final error output after implementation:

  ![Handling GraphQL Errors with Status Code](/sites/default/files/c3-body-media/25d28456c2cad5f2e798975a.avif) Here we see the error has the properties message and statusCode also we can add extra values to the error message in resolver.

### Conclusion

GraphQL does not describe errors in error handling. It’s up to you to decide what describes your errors.

Click here for the code link [gqlErrorHandling](https://github.com/archanaagivale30/gqlErrorHandling)

 



Written by

Archana Agivale

Tech Lead Javascript

 

 

 

 

 

 

 ## We'd love to talk about your business objectives

 [ Contact Us  ](/contact)