Thuta Learning
GraphQL
IntermediateWeb Developmentbeginner

Error Handling

What you'll walk away with

  • Explain the core ideas behind Error Handling
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

Where REST expresses errors through HTTP status codes (404, 500), a GraphQL response returns HTTP 200 and carries a separate `errors` array alongside `data`. If one field fails but sibling fields can still resolve, GraphQL returns a partial response containing both data and errors. The error object's `extensions` can carry a custom `code` (such as `NOT_FOUND` or `UNAUTHENTICATED`) so clients can handle errors programmatically.

Connect it to a real scenario

Querying tutorial 999, which does not exist, causes the resolver to throw a `GraphQLError` with `extensions.code: 'NOT_FOUND'`. The response's `data.tutorial` is `null`, `errors[0].path` is `["tutorial"]`, and the frontend checks `error.extensions.code` to show a "Tutorial not found" UI.

Try the working example

json
{
  "data": {
    "tutorial": null
  },
  "errors": [
    {
      "message": "Tutorial 999 not found",
      "path": ["tutorial"],
      "extensions": {
        "code": "NOT_FOUND"
      }
    }
  ]
}
You should see
You can read a GraphQL error response and write handling logic based on `extensions.code`.

5-minute try-it

Write the response JSON shape you'd expect when the `author` field's resolver throws a `GraphQLError` with `extensions.code: 'NOT_FOUND'`.

One important caution

Client code that only checks HTTP status can miss GraphQL errors entirely — an HTTP 200 response can still carry an `errors` array, so always check it.

Apollo Server — Handling GraphQL ErrorsGraphQL

Easy traps

  • Client code that only checks HTTP status can miss GraphQL errors entirely — an HTTP 200 response can still carry an `errors` array, so always check it.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Write the response JSON shape you'd expect when the `author` field's resolver throws a `GraphQLError` with `extensions.code: 'NOT_FOUND'`.

You'll know it worked when: You can read a GraphQL error response and write handling logic based on `extensions.code`.

Error Handling | Thuta Learning