bugsnag-js icon indicating copy to clipboard operation
bugsnag-js copied to clipboard

Feature request: Apollo server plugin

Open villesau opened this issue 3 years ago • 6 comments

Description

Apollo server is a popular framework and has a special way of handling errors. Would make sense if Bugsnag would have 1st party package handling Apollo server errors.

Describe the solution you'd like

Either a plugin, or a function passed to apollos formatError hook.

villesau avatar Mar 11 '22 12:03 villesau

Hi @villesau,

Thanks for the suggestion. We are already looking into ways to better support Apollo Server/GraphQL errors so this is definitely on our radar. We don't have any timeframes at this stage or exactly what it would look like but we will let you know of updates here.

johnkiely1 avatar Mar 18 '22 10:03 johnkiely1

Nice, thanks for the info!

I've tried this:

  formatError: error => {
    Bugsnag.notify(error);
    return error;
  },

But it does not work and needs improvement to capture stacktraces and error details. Apollo throws GraphQLError which holds the stacktrace differently. Here are further details of error handling: https://www.apollographql.com/docs/apollo-server/data/errors/

villesau avatar Mar 18 '22 14:03 villesau

@johnkiely1 something like this could be a good starting point:

const bugsnagPlugin = (): ApolloServerPlugin => ({
  requestDidStart: async () => ({
    async didEncounterErrors(requestContext) {
      requestContext.errors.forEach(error => {
        Bugsnag.notify(error, event => {
          event.addMetadata('GraphQLMetadata', {
            path: error.path
          });
        });
      });
    }
  })
});

See this for the reference: https://www.apollographql.com/docs/apollo-server/integrations/plugins-event-reference/#didencountererrors

There's likely plenty of good heuristics that could be added as a metadata.

villesau avatar Mar 19 '22 14:03 villesau

@johnkiely1 any news on this? Apollo is more popular than many of the other plugins provided so far.

villesau avatar Oct 06 '22 09:10 villesau

This is how we currently forward apollo errors to bugsnag:


const notifyGraphqlError = (
  error: Error,
  graphqlError: GraphQLError,
  request: GraphQLRequest
) => {
  Bugsnag.notify(error, event => {
    event.addMetadata('GraphQLMetadata', {
      path: graphqlError.path,
      source: graphqlError.source,
      graphqlError: error
    });

    event.addMetadata('request', {
      // @ts-expect-error raw not found
      headers: request?.http?.headers?.raw?.(),
      url: request?.http?.url
    });
  });
};

const bugsnagPlugin = (): ApolloServerPlugin => ({
  requestDidStart: async () => ({
    async didEncounterErrors(requestContext) {
      requestContext.errors.forEach(error => {
        console.log(error);
        notifyGraphqlError(
          // prefer reporting the original error over the more generic grpahql error
          error.originalError ?? error,
          error,
          requestContext.request
        );
      });
    }
  })
});

villesau avatar Oct 06 '22 09:10 villesau

Hi @villesau, unfortunately we don't have any updates at this time. This work is still on our backlog but we have not yet been able to get to it. We will update here as soon as we have any information to share.

johnkiely1 avatar Oct 07 '22 08:10 johnkiely1