ErrorLink
Handle and inspect errors in your GraphQL network stack.
Use the ErrorLink to perform custom logic when a GraphQL or network error
occurs.
This link is used after the GraphQL operation completes and execution is
moving back up your link chain. The errorHandler function should
not return a value unless you want to retry the operation.
For more information on the types of errors that might be encountered, see the guide on error handling.
1 import { ErrorLink } from "@apollo/client/link/error";
2 import {
3 CombinedGraphQLErrors,
4 CombinedProtocolErrors,
5 } from "@apollo/client/errors";
6
7 // Log any GraphQL errors, protocol errors, or network error that occurred
8 const errorLink = new ErrorLink(({ error, operation }) => {
9 if (CombinedGraphQLErrors.is(error)) {
10 error.errors.forEach(({ message, locations, path }) =>
11 console.log(
12 `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
13 )
14 );
15 } else if (CombinedProtocolErrors.is(error)) {
16 error.errors.forEach(({ message, extensions }) =>
17 console.log(
18 `[Protocol error]: Message: ${message}, Extensions: ${JSON.stringify(
19 extensions
20 )}`
21 )
22 );
23 } else {
24 console.error(`[Network error]: ${error}`);
25 }
26 });Constructor signature
1constructor(
2 errorHandler: ErrorLink.ErrorHandler
3): ErrorLinkTypes
Callback that is called by ErrorLink when an error occurs from a
downstream link in link chain.
Signature
1ErrorHandler(
2 options: ErrorHandlerOptions
3): Observable<ApolloLink.Result> | voidParameters
The options object provided by ErrorLink to the error
handler when an error occurs.
The object provided to the ErrorHandler callback function.
ErrorLikeThe error that occurred during the operation execution. This can be a
CombinedGraphQLErrors instance (for GraphQL errors) or another error
type (for network errors).
Use CombinedGraphQLErrors.is(error) to check if it's a GraphQL error with an errors array.
ApolloLink.ForwardFunctionA function that calls the next link in the link chain. Calling
return forward(operation) in your ErrorLink callback
retries the operation, returning a new observable for the
upstream link to subscribe to.
ApolloLink.OperationThe details of the GraphQL operation that produced an error.
ApolloLink.ResultThe raw GraphQL result from the server (if available), which may include partial data alongside errors.