diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index b2b638f81f935..5d422cf6136ab 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -1,24 +1,53 @@ -```javascript {filename: main.ts} {17} +```javascript {filename: main.ts} // Import this first! import './instrument'; // Now import other modules -import * as Sentry from "@sentry/nestjs"; -import { - BaseExceptionFilter, - HttpAdapterHost, - NestFactory -} from '@nestjs/core'; +import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - const { httpAdapter } = app.get(HttpAdapterHost); - - Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter)); - await app.listen(3000); } bootstrap(); ``` + +Then you can add the SentryModule as a root module: + +The SentryModule needs to be registered before any other module that should be instrumented by Sentry. + +```javascript {filename: app.module.ts} {2, 8} +import { Module } from '@nestjs/common'; +import { SentryModule } from '@sentry/nestjs/setup'; +import { AppController } from './app.controller'; +import { AppService } from './app.service'; + +@Module({ + imports: [ + SentryModule.forRoot(), + // ...other modules + ], + controllers: [AppController], + providers: [AppService], +}) +export class AppModule {} +``` + +By default, exceptions with status code 4xx are not sent to Sentry. If you still want to capture these exceptions, you can do so manually with `Sentry.captureException()`: + +```javascript {11} +import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; +import { BaseExceptionFilter } from '@nestjs/core'; +import { ExampleException } from './example.exception'; +import * as Sentry from '@sentry/nestjs'; + +@Catch(ExampleException) +export class ExampleExceptionFilter extends BaseExceptionFilter { + catch(exception: unknown, host: ArgumentsHost) { + Sentry.captureException(exception); + return super.catch(new BadRequestException(exception.message), host) + } +} +```