Deep PatelNavigate back to the homepage

How to Add Sentry to your Angular project with Graphql

Deep Patel
February 6th, 2022 · 1 min read

Sentry.io is an external monitoring and logging service which can help you identify and triage errors in your code.

These logs provide information such as a trace stack, breadcrumbs, and (assuming this is a web application) browser data. This can help you triage issues and resolve bugs faster, with less investigative overhead.


How to Create a Sentry Project

You can follow this walkthrough doc for creating Sentry project. Create new Sentry project


How to Add Sentry to yor Angular App

Follow this amazing guide to get you up and running with Sentry’s SDK in Angular App. Add Sentry to Angular App


How to log Graphql errors using Sentry

Best way to handle Graphql errors is using Error Links. Error Link allow you to log both GraphQL errors (errors returned as part of the response) as well as network errors (failed requests, invalid queries, etc.).

Use the onError link to perform custom logic when a GraphQL or network error occurs. You pass this link a function that’s executed if an operation returns one or more errors:

Inside onError link we can capture Graphql errors with Sentry Example use would be like this:

1import { onError } from "@apollo/client/link/error";
2import * as Sentry from "@sentry/angular";
3
4const errorHandlerLink = onError((err, ...param) => {
5 const { graphQLErrors, networkError } = err;
6 if (graphQLErrors)
7 graphQLErrors.map(({ message, locations, path }) => {
8 Sentry.captureMessage(message)
9 }
10 )
11 if (networkError) {
12 Sentry.captureException(networkError)
13 }
14});
15
16// Using errorHandlerLink while creating apollo instance
17apollo.create({
18 link: errorHandlerLink.concat(httpLink.create({ uri })),
19});

A complete example Graphql module file

1import { NgModule } from '@angular/core';
2import { APOLLO_OPTIONS } from 'apollo-angular';
3import { InMemoryCache } from '@apollo/client/core';
4import { HttpLink } from 'apollo-angular/http';
5import { environment } from 'src/environments/environment';
6import { onError } from "@apollo/client/link/error";
7import * as Sentry from "@sentry/angular";
8
9const uri = environment.apiEndpoint; // <-- add the URL of the GraphQL server here
10
11export function createApollo(httpLink: HttpLink) {
12
13 const errorHandlerLink = onError((err, ...param) => {
14 const { graphQLErrors, networkError } = err;
15 if (graphQLErrors)
16 graphQLErrors.map(({ message, locations, path }) => {
17 Sentry.captureMessage(message)
18 }
19 )
20 if (networkError) {
21 Sentry.captureException(networkError)
22 }
23 });
24
25 return {
26 link: errorHandlerLink.concat(httpLink.create({ uri })),
27 cache: new InMemoryCache({ addTypename: false }),
28 };
29}
30
31@NgModule({
32 providers: [
33 {
34 provide: APOLLO_OPTIONS,
35 useFactory: createApollo,
36 deps: [HttpLink],
37 },
38 ],
39})
40export class GraphQLModule { }

You have now successfully integrated Sentry with your Angular project. You are now ready to start receiving error information, triaging issues, and improving your project’s stability.

More articles from Deep Patel

What is the !! (not not) operator in JavaScript?

What is the !! (not not) operator in JavaScript?

August 1st, 2021 · 1 min read

Handling multiple API calls with Promise.allSettled()

Handling multiple API calls with Promise.allSettled().

January 17th, 2021 · 1 min read
© 2020–2022 Deep Patel
Link to $https://twitter.com/pateldeep_ethLink to $https://github.com/deep1144Link to $https://www.instagram.com/deep__1144/Link to $https://www.linkedin.com/in/patel-deep-dev/Link to $https://dribbble.com/Deep1144