Error Link
Handle and inspect errors in your GraphQL network stack.
We recommend reading Apollo Link overview before learning about individual links.
Use the onError
link to perform custom logic when a GraphQL or network error occurs. You pass this link a function that's executed if an operation returns one or more errors:
1import { onError } from "@apollo/client/link/error";
2
3// Log any GraphQL errors or network error that occurred
4const errorLink = onError(({ graphQLErrors, networkError }) => {
5 if (graphQLErrors)
6 graphQLErrors.forEach(({ message, locations, path }) =>
7 console.log(
8 `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
9 )
10 );
11 if (networkError) console.log(`[Network error]: ${networkError}`);
12});
This function is called after the GraphQL operation completes and execution is moving back up your link chain. The function should not return a value unless you want to retry the operation.
Options
The function you provide the onError
link is passed an object with the following fields:
Name / Type |
Description |
---|---|
|
The details of the GraphQL operation that produced an error. |
|
The (possibly modified) GraphQL result from the server, passed by the next link down the chain (i.e., the link closer to the terminating link). |
|
An array of GraphQL errors that occurred while executing the operation, if any. |
|
A network error that occurred while attempting to execute the operation, if any. |
|
A function that calls the next link down the chain. Calling |
Error categorization
An error is passed as a networkError
if a link further down the chain called the error
callback on the observable. In most cases, graphQLErrors
is the errors
field of the result from the last next
call.
A networkError
can contain additional fields, such as a GraphQL object in the case of a failing HTTP status code. In this situation, graphQLErrors
is an alias for networkError.result.errors
if the property exists.