Course contentsShow
← Web Development
Lesson 1552 of 2,98228. GraphQL FundamentalsFree lesson

Error Handling Patterns in REST vs GraphQL

Comparing REST's HTTP status codes with GraphQL's structured errors within 200 responses.

Error Handling Patterns in REST vs GraphQL

You'll learn: How REST and GraphQL communicate errors differently—one through HTTP status codes, the other through structured data within successful responses.

When building APIs, how you signal errors matters enormously for client debugging and user experience. REST and GraphQL take fundamentally different approaches.

REST's HTTP Status Code Approach

In REST, errors are communicated primarily through HTTP status codes. If something goes wrong, the server responds with an appropriate status code:

  • 400 for bad request syntax
  • 401 for authentication failures
  • 404 for missing resources
  • 500 for server crashes

The entire response is considered "unsuccessful" when you receive a 4xx or 5xx code. Your client code typically checks response.status or handles promise rejections:

if (!response.ok) {
  throw new Error(`HTTP ${response.status}`);
}

Think of it like traffic lights: Red means stop, everything failed. Green means go, everything succeeded.

GraphQL's Structured Error Approach

GraphQL always returns a 200 OK status (unless the server itself is down). Instead, errors live inside the JSON response body alongside your data:

{
  "data": { "user": null },
  "errors": [
    {
      "message": "User not found",
      "path": ["user"],
      "extensions": { "code": "NOT_FOUND" }
    }
  ]
}

This means partial success is possible—some fields succeed while others fail in the same response. Your client checks response.errors even when the HTTP status is successful.

Think of it like a report card: You get the whole document (200 OK), but individual grades might be failing marks.

Why the Difference?

REST treats each request as a single operation (pass/fail). GraphQL treats requests as potentially multiple operations (complex queries with many fields), so it needs granular error reporting within the response structure.

Key Takeaway: REST uses HTTP status codes to signal error states, while GraphQL embeds structured error information within 200 responses, enabling partial success and field-level error reporting.