bugsnag-js
bugsnag-js copied to clipboard
Feature request: Apollo server plugin
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.
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.
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/
@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.
@johnkiely1 any news on this? Apollo is more popular than many of the other plugins provided so far.
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
);
});
}
})
});
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.