ApolloClient crashes entire next app on error

I’m using ApolloClient with nextjs and some getStaticProps configured for ISR. I keep running in to a situation where it crashes my entire server when I get an error, or at least that’s what I assume is happening, based on logs - it always puts out an ApolloClient error, then crashes the server.

How can I make sure an ApolloClient error doesn’t knock out the whole server?

This is my configuration:


const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.forEach(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
      ),
    );
  if (networkError) console.log(`[Network error]: ${networkError}`);
});

function createApolloClient() {
  const linkBase = [errorLink];
  if (typeof window !== "undefined") {
    linkBase.push(apolloLogger);
  }
  return new ApolloClient({
    ssrMode: typeof window === "undefined",
    link: ApolloLink.from([
      ...linkBase,
      createPersistedQueryLink({
        sha256,
        useGETForHashedQueries: true,
      }),
      createHttpLink({
        uri: process.env.NEXT_PUBLIC_GQL_API_URL,
        credentials: "include",
      }),
    ]),
    cache: new InMemoryCache({
      addTypename: true,
      typePolicies: {
        ...
      },
    }),
  });
}

Hi @CaptainN :wave: welcome to the Apollo GraphQL Forum! I believe node.js crashes on any uncaught error or rejected promise. If you share more info about your setup I might be able to get more specific but in general I recommend looking at the stack trace to see where the error is originating. You may want to consider try/catch blocks or other ways of handling errors/rejected promises. I hope that helps!

It’s just strange because it only happens in node 18 - node 16 is fine. I also have an error handler, that should be capturing that. I’ll have to look and see what changed.

That’s interesting that the node version makes a difference. I wonder what might be going on there?

Maybe it has something to do with node now shipping it’s own fetch implementation?