Skip to content

Commit da1b005

Browse files
committed
Add section on server error/server parse error
1 parent ff3e07b commit da1b005

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

docs/source/migrating/apollo-client-4-migration.mdx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,11 +1208,43 @@ observable.subscribe({
12081208
});
12091209
```
12101210
1211-
### TODO
1211+
### `ServerError` and `ServerParseError` are error classes
12121212
1213-
In Apollo Client 4,
1213+
Apollo Client 3 threw special `ServerError` and `ServerParseError` types when the HTTP response was invalid. These types were plain `Error` instances with some added properties.
12141214
1215-
- a few other error classes have been added to represent specific error cases, such as `ServerParseError`, and `ServerError`.
1215+
Apollo Client 4 turns these types into proper error classes. You can check for these types using the `.is` static method available on each error class.
1216+
1217+
```ts disableCopy=true
1218+
const { error } = useQuery(QUERY);
1219+
1220+
if (ServerError.is(error)) {
1221+
// handle the server error
1222+
}
1223+
1224+
if (ServerParseError.is(error)) {
1225+
// handle the server parse error
1226+
}
1227+
```
1228+
1229+
### `ServerError` no longer parses the response text as JSON
1230+
1231+
When a `ServerError` was thrown, such as when the server returned a non-2xx status code, Apollo Client 3 tried to parse the raw response text into JSON to determine if a valid GraphQL response was returned. If successful, the parsed JSON value was set as the `result` property on the error.
1232+
1233+
Apollo Client 4 more strictly adheres to the [GraphQL over HTTP specification](https://graphql.github.io/graphql-over-http/draft/) and does away with the automatic JSON parsing. As such, the `result` property is no longer accessible and is replaced by the `bodyText` property which contains the value of the raw response text.
1234+
1235+
If you relied on the automatic JSON parsing, you will need to `JSON.parse` the `bodyText` intead.
1236+
1237+
```ts disableCopy=true
1238+
const { error } = useQuery(QUERY);
1239+
1240+
if (ServerError.is(error)) {
1241+
try {
1242+
const json = JSON.parse(error.bodyText);
1243+
} catch (e) {
1244+
// handle invalid JSON
1245+
}
1246+
}
1247+
```
12161248
12171249
### React-specific changes
12181250

0 commit comments

Comments
 (0)