DelGiudice
DelGiudice

Reputation: 1877

How can I send Apollo Server errors to Sentry?

I'm trying to log errors to sentry but all I have access to is formatError option which doesn't include the native error so that sentry can display the stacktrace and more..

Upvotes: 2

Views: 3044

Answers (2)

AndiH
AndiH

Reputation: 602

This blog article helped me to solve exactly this problem. The idea of GraphQL is, that your client has to handle possible errors. That's why Apollo catches them and Sentry gets no chance of dealing with them.

The article introduces a custom plugin that is linked into the process chain of apollo server and notifies Sentry if an error occurs. The code is pretty straight forward. I could easily implement it in my NestJS application.

Upvotes: 0

Daniel Rearden
Daniel Rearden

Reputation: 84717

The error passed to formatError is a GraphQLError, which wraps the actual execution error. The error has a number of useful properties (see the source here), including an originalError property that exposes, well, the original error. Do note, however, that this will only be populated for execution errors, i.e. errors that get thrown inside a resolver. Other errors, notably validation errors, will not have this field, but will still be passed to formatError.

function formatError(error) {
  console.log(error.originalError)
  return error
}

Aside from using formatError, you may find this package useful, which used with graphql-middleware, will do the heavy lifting for you.

Upvotes: 5

Related Questions