Marcelo Zapatta
Posted on April 27, 2023
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
2. Save the URL generated in the DNS section:
3. Install the dependencies
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
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:
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:
// 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
2. Save the URL generated in the DNS section:
3. Install the dependencies
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
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:
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:
// 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
Posted on April 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.