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:
400for bad request syntax401for authentication failures404for missing resources500for 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.