I've got a react app using Apollo to retrieve data from a graphql endpoint.
I have an error link method set up to show different messages if an http error is returned from the GQL, which works fine for 4xx errors. However, if the server fails with a 5xx error, I get a generic "failed to fetch" error message in my method, but I don't get an error code.
The 503 error usually indicates that we're in maintenance mode, so I want to be able to show a different message if we get a 503, vs any other 5xx error. Is there a way I can adjust my error handling to get the code for 5xx errors? I can't see anything obvious in the errorResponse being passed to the method.
Here's a truncated version of my onError link:
const errorLink = onError((errorResponse) => {
const { graphQLErrors, networkError, operation } = errorResponse;
// Doesn't work - networkError is null.
if ((
networkError?.statusCode === 503
) && !window.location.pathname.includes('maintenance-mode')
) {
// [show maintenance mode page]
}
// This works.
if (networkError?.statusCode === 401 && operation?.operationName !== 'auth') {
// [redirect to login page]
}
// [...etc]
});