NestJS Sentry Integration Module

10.1.0 · active · verified Sun Apr 19

nest-raven is a module designed to seamlessly integrate Sentry error tracking into applications built with the NestJS framework. It replaces the deprecated `raven` package with the modern `@sentry/node` SDK, providing a robust solution for capturing exceptions. Currently stable at v10.1.0, the package typically releases updates in alignment with major NestJS framework versions, alongside regular dependency maintenance. While it offers a quick starter for common REST/GraphQL error capturing via interceptors and filters, the documentation advises that for large-scale projects requiring deeper Sentry integration beyond basic error handling, developers might consider using this library as a reference to implement a custom solution tailored to their specific needs. Its primary differentiators are its NestJS-native interceptor approach and a clear migration path from older Sentry integration methods.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Sentry, integrate `RavenModule`, and use `RavenInterceptor` both locally on a route with filters (to ignore client errors) and globally for all controllers.

import { NestFactory } from '@nestjs/core';
import { Module, NestModule, UseInterceptors, Get, HttpException, Controller } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { RavenModule, RavenInterceptor } from 'nest-raven';
import * as Sentry from '@sentry/node';

// Initialize Sentry SDK early in your application lifecycle
Sentry.init({
  dsn: process.env.SENTRY_DSN ?? 'YOUR_SENTRY_DSN_HERE',
  tracesSampleRate: 1.0,
});

@Controller()
class AppController {
  @UseInterceptors(new RavenInterceptor({
    filters: [
      { type: HttpException, filter: (exception: HttpException) => exception.getStatus() < 500 }
    ],
    // Example transformer to add custom data to Sentry scope
    // transformer: (scope, context) => {
    //   const http = context.getType() === 'http' ? context.switchToHttp() : null;
    //   if (http) {
    //     const request = http.getRequest();
    //     scope.setExtra('customRequestData', { url: request.url, method: request.method });
    //   }
    //   return scope;
    // }
  }))
  @Get('/error')
  public async triggerError() {
    throw new Error('This is a test error to be captured by Sentry!');
  }

  @Get('/client-error')
  public async clientError() {
    throw new new HttpException('This is a client error (400 level)', 400);
  }

  @Get('/server-error')
  public async serverError() {
    throw new new HttpException('This is a server error (500 level)', 500);
  }
}

@Module({
  imports: [RavenModule],
  controllers: [AppController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useValue: new RavenInterceptor(), // Global interceptor without filters
    },
  ],
})
export class ApplicationModule {}

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);
  await app.listen(3000);
  console.log('Application is running on: http://localhost:3000');
  console.log('Visit /error, /client-error, /server-error to trigger errors.');
}

bootstrap();

view raw JSON →