{"id":11383,"library":"nest-raven","title":"NestJS Sentry Integration Module","description":"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.","status":"active","version":"10.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/mentos1386/nest-raven","tags":["javascript","nestjs","nest","raven","sentry","module","typescript"],"install":[{"cmd":"npm install nest-raven","lang":"bash","label":"npm"},{"cmd":"yarn add nest-raven","lang":"bash","label":"yarn"},{"cmd":"pnpm add nest-raven","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core NestJS framework dependency, required for module and interceptor functionality.","package":"@nestjs/common","optional":false},{"reason":"The underlying Sentry SDK for Node.js environments, essential for error reporting.","package":"@sentry/node","optional":false},{"reason":"Used internally by NestJS interceptors for reactive programming patterns.","package":"rxjs","optional":false}],"imports":[{"note":"Use ES Module imports. CommonJS `require` is generally not idiomatic for modern NestJS applications, which are typically TypeScript and ESM-first.","wrong":"const { RavenModule } = require('nest-raven');","symbol":"RavenModule","correct":"import { RavenModule } from 'nest-raven';"},{"note":"RavenInterceptor is a named export, not a default export.","wrong":"import RavenInterceptor from 'nest-raven';","symbol":"RavenInterceptor","correct":"import { RavenInterceptor } from 'nest-raven';"},{"note":"While used with `nest-raven`, this is a core NestJS token for registering global interceptors, not directly from `nest-raven`.","symbol":"APP_INTERCEPTOR","correct":"import { APP_INTERCEPTOR } from '@nestjs/core';"}],"quickstart":{"code":"import { NestFactory } from '@nestjs/core';\nimport { Module, NestModule, UseInterceptors, Get, HttpException, Controller } from '@nestjs/common';\nimport { APP_INTERCEPTOR } from '@nestjs/core';\nimport { RavenModule, RavenInterceptor } from 'nest-raven';\nimport * as Sentry from '@sentry/node';\n\n// Initialize Sentry SDK early in your application lifecycle\nSentry.init({\n  dsn: process.env.SENTRY_DSN ?? 'YOUR_SENTRY_DSN_HERE',\n  tracesSampleRate: 1.0,\n});\n\n@Controller()\nclass AppController {\n  @UseInterceptors(new RavenInterceptor({\n    filters: [\n      { type: HttpException, filter: (exception: HttpException) => exception.getStatus() < 500 }\n    ],\n    // Example transformer to add custom data to Sentry scope\n    // transformer: (scope, context) => {\n    //   const http = context.getType() === 'http' ? context.switchToHttp() : null;\n    //   if (http) {\n    //     const request = http.getRequest();\n    //     scope.setExtra('customRequestData', { url: request.url, method: request.method });\n    //   }\n    //   return scope;\n    // }\n  }))\n  @Get('/error')\n  public async triggerError() {\n    throw new Error('This is a test error to be captured by Sentry!');\n  }\n\n  @Get('/client-error')\n  public async clientError() {\n    throw new new HttpException('This is a client error (400 level)', 400);\n  }\n\n  @Get('/server-error')\n  public async serverError() {\n    throw new new HttpException('This is a server error (500 level)', 500);\n  }\n}\n\n@Module({\n  imports: [RavenModule],\n  controllers: [AppController],\n  providers: [\n    {\n      provide: APP_INTERCEPTOR,\n      useValue: new RavenInterceptor(), // Global interceptor without filters\n    },\n  ],\n})\nexport class ApplicationModule {}\n\nasync function bootstrap() {\n  const app = await NestFactory.create(ApplicationModule);\n  await app.listen(3000);\n  console.log('Application is running on: http://localhost:3000');\n  console.log('Visit /error, /client-error, /server-error to trigger errors.');\n}\n\nbootstrap();","lang":"typescript","description":"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."},"warnings":[{"fix":"Update all `@nestjs/*` peer dependencies to `^10.0.0` or newer versions.","message":"Version 10.0.0 of `nest-raven` requires NestJS v10.0.0 or higher. Ensure your `@nestjs/common` and other core NestJS packages are updated accordingly.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Ensure all `@nestjs/*` peer dependencies are updated to `^8.0.0`.","message":"Version 8.0.0 of `nest-raven` introduced a breaking change by upgrading to NestJS v8. This required corresponding updates in your NestJS application dependencies.","severity":"breaking","affected_versions":">=8.0.0 <9.0.0"},{"fix":"Add `Sentry.init({ dsn: 'YOUR_SENTRY_DSN' });` at the top level of your `main.ts`.","message":"The Sentry SDK (`@sentry/node`) must be initialized manually using `Sentry.init()` in your application's `main.ts` file or similar entry point, *before* NestJS application bootstrap. `nest-raven` does not handle this initialization.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For WebSocket Gateways, implement a custom exception filter or apply `RavenInterceptor` directly to the Gateway methods using `@UseInterceptors(new RavenInterceptor())` if supported by NestJS for that specific context.","message":"When `RavenInterceptor` is applied globally using `APP_INTERCEPTOR`, it only captures exceptions from HTTP controllers. It does not provide error capturing for WebSockets (Gateways) due to a limitation in how NestJS applies global interceptors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For complex scenarios, consider using this library as a guide and implementing a custom Sentry integration tailored to your specific application architecture.","message":"This module is described as a 'quick starter' and may not be sufficient for large applications requiring deep Sentry integration beyond basic REST/GraphQL error capturing. Custom Sentry integrations may be necessary.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Add `Sentry.init({ dsn: process.env.SENTRY_DSN });` to your `main.ts` file at the top, before `NestFactory.create()`.","cause":"The `@sentry/node` SDK was not initialized with `Sentry.init()` before the NestJS application started or before an error occurred.","error":"Error: Sentry SDK is not initialized, call Sentry.init() first."},{"fix":"Install `@sentry/node`: `npm install @sentry/node` or `yarn add @sentry/node`.","cause":"`@sentry/node` is a peer dependency of `nest-raven` but was not installed in your project.","error":"Error: Can't resolve '@sentry/node' in 'node_modules/nest-raven/dist'"},{"fix":"Safely check the context type within your transformer: `const http = context.getType() === 'http' ? context.switchToHttp() : null;`","cause":"This error can occur in a custom `transformer` function if the `context` passed to it is not an HTTP context (e.g., from a GraphQL execution context) and the code assumes `switchToHttp()` is always available without checking.","error":"TypeError: Cannot read properties of undefined (reading 'switchToHttp')"}],"ecosystem":"npm"}