Skip to content

Commit d7b5f26

Browse files
committed
Light editorial for #4382
1 parent 788d1a3 commit d7b5f26

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

website/pages/docs/graphql-errors.mdx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Understanding GraphQL.js Errors
33
---
4+
import { Callout, GitHubNoteIcon } from "nextra/components";
45

56
# Understanding GraphQL.js Errors
67

@@ -37,13 +38,22 @@ For example:
3738
Each error object can include the following fields:
3839

3940
- `message`: A human-readable description of the error.
40-
- `locations` (optional): Where the error occurred in the operation.
41+
- `locations` (optional): Where the error occurred in the operation document.
4142
- `path` (optional): The path to the field that caused the error.
4243
- `extensions` (optional): Additional error metadata, often used for error codes, HTTP status
4344
codes or debugging information.
4445

45-
The GraphQL specification only requires the `message` field. All others are optional, but
46-
recommended to help clients understand and react to errors.
46+
<Callout type="info">
47+
48+
The GraphQL specification separates errors into two types: _request_ errors, and
49+
_execution_ errors. Request errors indicate something went wrong that prevented
50+
the GraphQL operation from executing, for example the document is invalid, and
51+
only requires the `message` field. Execution errors indicate something went
52+
wrong during execution, typically due to the result of calling a resolver, and
53+
requires both the `message` and `path` fields to be present. All others fields
54+
are optional, but recommended to help clients understand and react to errors.
55+
56+
</Callout>
4757

4858
## Creating and handling errors with `GraphQLError`
4959

@@ -81,12 +91,16 @@ Each option helps tie the error to specific parts of the GraphQL execution:
8191

8292
When a resolver throws an error:
8393

84-
- If the thrown value is already a `GraphQLError`, GraphQL.js uses it as-is.
85-
- If it is another type of error (such as a built-in `Error`), GraphQL.js wraps it into a
86-
`GraphQLError`.
94+
- If the thrown value is a `GraphQLError` and contains the required information
95+
(`path`), GraphQL.js uses it as-is.
96+
- Otherwise, GraphQL.js wraps it into a `GraphQLError`.
8797

8898
This ensures that all errors returned to the client follow a consistent structure.
8999

100+
You may throw any type of error that makes sense in your application; throwing
101+
`Error` is fine, you do not need to throw `GraphQLError`. However, ensure that
102+
your errors do not reveal security sensitive information.
103+
90104
## How errors propagate during execution
91105

92106
Errors in GraphQL don't necessarily abort the entire operation. How an error affects the response
@@ -96,6 +110,7 @@ depends on the nullability of the field where the error occurs.
96110
the error and sets the field's value to `null` in the `data` payload.
97111
- **Non-nullable fields**: If a resolver for a non-nullable field throws an error, GraphQL.js
98112
records the error and then sets the nearest parent nullable field to `null`.
113+
If no such nullable field exists, then the operation root will be set `null` (`"data": null`).
99114

100115
For example, consider the following schema:
101116

0 commit comments

Comments
 (0)