How to integrate Sentry in NestJS
Tags: webdev, nestjs, node, sentry
Published: 2023-04-27 16:38:40

Hi guys, how are you? As Sentry does not have native support for the NestJS framework, I have created this step-by-step guide on how to integrate it. There are some tutorials using Nest's Interceptors, but the correct approach would be to use ExceptionFilters because Sentry only captures Exceptions, and it makes the code much simpler and easier to configure 😊

**1. First, create a project in Sentry as NodeJS**

![Sentry page to create a project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot3i5ygpp5pjefzj3eho.png)

**2. Save the URL generated in the DNS section:**

![Location in Sentry to get the DNS URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf2xo9lyqdefliwfe09f.png)

**3. Install the dependencies**

```javascript
yarn add @sentry/node @sentry/tracing

// or

npm install @sentry/node @sentry/tracing
```

**4. Include a SENTRY_DNS key in your .env file containing the URL of your project:**

Use the URL obtained in step 2 after generating the project

```bash
SENTRY_DNS=https://xxx@xxx.ingest.sentry.io/xxx
```

**5. Create an ExceptionFilter**

We will create an _**ExceptionFilter**_ inside Nest. To do this, include a file called sentry.filter.ts in the src folder with the following content:

```javascript
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';


@Catch()
export class SentryFilter extends BaseExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    Sentry.captureException(exception);
    super.catch(exception, host);
  }
}
```

We are generating a generic **_ExceptionFilter_** that captures any type of **_Exception_** because the **_@Catch()_** decorator is empty. Also, we are extending it from **_BaseExceptionFilter_**, which is Nest's default class for handling exceptions. Therefore, we will not change the original behavior when an exception is thrown because the method of the parent class that handles exceptions is called at the end of the code: **_super.catch(exception, host);_**

**6. Include the Sentry script in Nest's main.ts**

Now simply call the Sentry initialization method and import the globally created **_ExceptionFilter_** in your **_main.ts_** file's **_bootstrap()_** method:

```javascript
// other importers
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import * as Sentry from '@sentry/node';
import { SentryFilter } from './filters/sentry.filter';


async function bootstrap() {
  // …
  // Initialize Sentry by passing the DNS included in the .env
  Sentry.init({
    dsn: process.env.SENTRY_DNS,
  });


  // Import the filter globally, capturing all exceptions on all routes
  const { httpAdapter } = app.get(HttpAdapterHost);
  app.useGlobalFilters(new SentryFilter(httpAdapter));

  // …
}


bootstrap();
```

Done! Configuration completed 🎉🎉. This way, we are capturing all the exceptions in the system, but you can customize the **_ExceptionFilter_** to block and obtain only specific exceptions.

**References**

Sentry docs Node.JS: https://docs.sentry.io/platforms/node/
ExceptionFilters NestJS: https://docs.nestjs.com/exception-filtersHi guys, how are you? As Sentry does not have native support for the NestJS framework, I have created this step-by-step guide on how to integrate it. There are some tutorials using Nest's Interceptors, but the correct approach would be to use ExceptionFilters because Sentry only captures Exceptions, and it makes the code much simpler and easier to configure 😊

**1. First, create a project in Sentry as NodeJS**

![Sentry page to create a project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot3i5ygpp5pjefzj3eho.png)

**2. Save the URL generated in the DNS section:**

![Location in Sentry to get the DNS URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf2xo9lyqdefliwfe09f.png)

**3. Install the dependencies**

```javascript
yarn add @sentry/node @sentry/tracing

// or

npm install @sentry/node @sentry/tracing
```

**4. Include a SENTRY_DNS key in your .env file containing the URL of your project:**

Use the URL obtained in step 2 after generating the project

```bash
SENTRY_DNS=https://xxx@xxx.ingest.sentry.io/xxx
```

**5. Create an ExceptionFilter**

We will create an _**ExceptionFilter**_ inside Nest. To do this, include a file called sentry.filter.ts in the src folder with the following content:

```javascript
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import * as Sentry from '@sentry/node';


@Catch()
export class SentryFilter extends BaseExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    Sentry.captureException(exception);
    super.catch(exception, host);
  }
}
```

We are generating a generic **_ExceptionFilter_** that captures any type of **_Exception_** because the **_@Catch()_** decorator is empty. Also, we are extending it from **_BaseExceptionFilter_**, which is Nest's default class for handling exceptions. Therefore, we will not change the original behavior when an exception is thrown because the method of the parent class that handles exceptions is called at the end of the code: **_super.catch(exception, host);_**

**6. Include the Sentry script in Nest's main.ts**

Now simply call the Sentry initialization method and import the globally created **_ExceptionFilter_** in your **_main.ts_** file's **_bootstrap()_** method:

```javascript
// other importers
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import * as Sentry from '@sentry/node';
import { SentryFilter } from './filters/sentry.filter';


async function bootstrap() {
  // …
  // Initialize Sentry by passing the DNS included in the .env
  Sentry.init({
    dsn: process.env.SENTRY_DNS,
  });


  // Import the filter globally, capturing all exceptions on all routes
  const { httpAdapter } = app.get(HttpAdapterHost);
  app.useGlobalFilters(new SentryFilter(httpAdapter));

  // …
}


bootstrap();
```

Done! Configuration completed 🎉🎉. This way, we are capturing all the exceptions in the system, but you can customize the **_ExceptionFilter_** to block and obtain only specific exceptions.

**References**

Sentry docs Node.JS: https://docs.sentry.io/platforms/node/
ExceptionFilters NestJS: https://docs.nestjs.com/exception-filters