I'm trying to implement a global error handling for a React application, which uses Apollo Client 3.4.8.
I'm setting up the App and Apollo like this:
const client = new ApolloClient({
cache: cacheInstance,
credentials: 'include',
uri: configuration.API,
typeDefs,
});
ReactDOM.render(
<StrictMode>
<ApolloProvider client={client}>
<App />
</ApolloProvider>
</StrictMode>,
document.getElementById('root')
);
Later on inside the <App /> I add an errorLink:
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
message.error('GraphqlError');
}
if (networkError) {
message.error('NetworkError');
}
});
client.setLink(from([errorLink, client.link]));
When the server responses with 502 Bad Gateway the error handler gets called twice for the same error.
The first time from http/createHttpLink.ts:
...
observer.next(err.result);
}
observer.error(err);
});
return () => {
...
... and the second time from error/index.ts:
...
return;
}
observer.error(networkError);
},
complete: () => {
...
Why is this happening that onError is called twice and how to prevent it?
I suspect that I set up the links wrongly?
It' seem like something is wrong with the link chain:
console.log(client.link);
client.setLink(from([errorLink, client.link]));
console.log(client.link);
