From 5427ba18eb4c74a4af2f8dfa509da5017e0727ef Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Thu, 24 Apr 2025 11:52:21 +0200 Subject: [PATCH 1/7] review and update Nest.js Quick Start guide --- .../javascript/common/install/commonjs.mdx | 116 +++++++++++++++++- .../javascript/guides/nestjs/index.mdx | 4 +- .../javascript.nestjs.mdx | 27 ++-- .../getting-started-node/javascript.mdx | 1 + .../getting-started-use/javascript.nestjs.mdx | 70 ++--------- .../javascript.nestjs.mdx | 42 ++++++- 6 files changed, 185 insertions(+), 75 deletions(-) diff --git a/docs/platforms/javascript/common/install/commonjs.mdx b/docs/platforms/javascript/common/install/commonjs.mdx index b82ef45c201e0..f1ae5cd08ebf2 100644 --- a/docs/platforms/javascript/common/install/commonjs.mdx +++ b/docs/platforms/javascript/common/install/commonjs.mdx @@ -27,4 +27,118 @@ You need to create a file named `instrument.js` that imports and initializes Sen You need to require or import the `instrument.js` file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application: - +```javascript {filename: main.ts} +// Import this first! +import "./instrument"; + +// Now import other modules +import { NestFactory } from "@nestjs/core"; +import { AppModule } from "./app.module"; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + await app.listen(3000); +} + +bootstrap(); +``` + +Afterwards, add the `SentryModule` as a root module to your main module: + +```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 {} +``` + +If you're using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. +This decorator will report all unexpected errors that are received by your global error filter to Sentry: + +```javascript {2, 6} +import { Catch, ExceptionFilter } from '@nestjs/common'; +import { SentryExceptionCaptured } from '@sentry/nestjs'; + +@Catch() +export class YourCatchAllExceptionFilter implements ExceptionFilter { + @SentryExceptionCaptured() + catch(exception, host): void { + // your implementation here + } +} +``` + +By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. +`HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. + +If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. +This filter will report any unhandled errors that aren't caught by other error filters to Sentry. +**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters. + +```javascript {3, 9} +import { Module } from "@nestjs/common"; +import { APP_FILTER } from "@nestjs/core"; +import { SentryGlobalFilter } from "@sentry/nestjs/setup"; + +@Module({ + providers: [ + { + provide: APP_FILTER, + useClass: SentryGlobalFilter, + }, + // ..other providers + ], +}) +export class AppModule {} +``` + + + +If you are using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters). +`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. + +Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: + +```typescript +import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; +import { Observable, throwError } from "rxjs"; +import { RpcException } from "@nestjs/microservices"; +import * as Sentry from "@sentry/nestjs"; + +@Catch(RpcException) +export class ExceptionFilter implements RpcExceptionFilter { + catch(exception: RpcException, host: ArgumentsHost): Observable { + Sentry.captureException(exception); // optional + return throwError(() => exception.getError()); + } +} +``` + + + +If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: + +```javascript {9} +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) + } +} +``` diff --git a/docs/platforms/javascript/guides/nestjs/index.mdx b/docs/platforms/javascript/guides/nestjs/index.mdx index 44deaaac34090..4d79a147016fd 100644 --- a/docs/platforms/javascript/guides/nestjs/index.mdx +++ b/docs/platforms/javascript/guides/nestjs/index.mdx @@ -1,6 +1,6 @@ --- title: Nest.js -description: "Learn about using Sentry with Nest.js." +description: "Learn how to set up Sentry in your Nest.js app and capture your first errors." sdk: sentry.javascript.nestjs fallbackGuide: javascript.node categories: @@ -9,6 +9,4 @@ categories: - server-node --- -This guide explains how to set up Sentry in your Nest.js application. - diff --git a/platform-includes/getting-started-config/javascript.nestjs.mdx b/platform-includes/getting-started-config/javascript.nestjs.mdx index 432dde267a99f..f927dbf398f41 100644 --- a/platform-includes/getting-started-config/javascript.nestjs.mdx +++ b/platform-includes/getting-started-config/javascript.nestjs.mdx @@ -1,17 +1,17 @@ ```javascript {tabTitle:ESM} {filename: instrument.mjs} import * as Sentry from "@sentry/nestjs"; // ___PRODUCT_OPTION_START___ profiling -import { nodeProfilingIntegration } from '@sentry/profiling-node'; +import { nodeProfilingIntegration } from "@sentry/profiling-node"; // ___PRODUCT_OPTION_END___ profiling // Ensure to call this before importing any other modules! Sentry.init({ dsn: "___PUBLIC_DSN___", - + // Adds request headers and IP for users, for more info visit: // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#sendDefaultPii sendDefaultPii: true, - + // ___PRODUCT_OPTION_START___ profiling integrations: [ // Add our Profiling integration @@ -20,14 +20,20 @@ Sentry.init({ // ___PRODUCT_OPTION_END___ profiling // ___PRODUCT_OPTION_START___ performance - // Add Tracing by setting tracesSampleRate + // Set tracesSampleRate to 1.0 to capture 100% + // of transactions for tracing. // We recommend adjusting this value in production + // Learn more at + // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate tracesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ performance // ___PRODUCT_OPTION_START___ profiling - // Set sampling rate for profiling + // Set profilesSampleRate to 1.0 to profile 100% + // of sampled transactions. // This is relative to tracesSampleRate + // Learn more at + // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate profilesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ profiling }); @@ -50,16 +56,21 @@ Sentry.init({ // ___PRODUCT_OPTION_END___ profiling // ___PRODUCT_OPTION_START___ performance - // Add Tracing by setting tracesSampleRate + // Set tracesSampleRate to 1.0 to capture 100% + // of transactions for tracing. // We recommend adjusting this value in production + // Learn more at + // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate tracesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ performance // ___PRODUCT_OPTION_START___ profiling - // Set sampling rate for profiling + // Set profilesSampleRate to 1.0 to profile 100% + // of sampled transactions. // This is relative to tracesSampleRate + // Learn more at + // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate profilesSampleRate: 1.0, // ___PRODUCT_OPTION_END___ profiling }); ``` - diff --git a/platform-includes/getting-started-node/javascript.mdx b/platform-includes/getting-started-node/javascript.mdx index 28b9ba96f53f5..1b4fae63214de 100644 --- a/platform-includes/getting-started-node/javascript.mdx +++ b/platform-includes/getting-started-node/javascript.mdx @@ -85,6 +85,7 @@ Our next recommended steps for you are: - Check out alternative installation methods +- Find various topics in Troubleshooting - [Get support](https://sentry.zendesk.com/hc/en-us/) diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index e18fc00fb03a2..65b8e320f2a29 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -14,7 +14,7 @@ async function bootstrap() { bootstrap(); ``` -Afterwards, add the `SentryModule` as a root module to your main module: +Afterward, add the `SentryModule` as a root module to your main module: ```javascript {filename: app.module.ts} {2, 8} import { Module } from "@nestjs/common"; @@ -33,28 +33,15 @@ import { AppService } from "./app.service"; export class AppModule {} ``` -If you're using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. -This decorator will report all unexpected errors that are received by your global error filter to Sentry: +By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are not captured automatically as they mostly act as control flow vehicles. -```javascript {2, 6} -import { Catch, ExceptionFilter } from '@nestjs/common'; -import { SentryExceptionCaptured } from '@sentry/nestjs'; +##### Basic Error Capture -@Catch() -export class YourCatchAllExceptionFilter implements ExceptionFilter { - @SentryExceptionCaptured() - catch(exception, host): void { - // your implementation here - } -} -``` +Add the `SentryGlobalFilter` to the providers of your main module. This filter will report any unhandled errors that aren't caught by other error filters to Sentry. -By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. -`HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. - -If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. -This filter will report any unhandled errors that aren't caught by other error filters to Sentry. -**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters. + + Make sure to register `SentryGlobalFilter` before any other exception filters. + ```javascript {3, 9} import { Module } from "@nestjs/common"; @@ -73,45 +60,6 @@ import { SentryGlobalFilter } from "@sentry/nestjs/setup"; export class AppModule {} ``` - - -If you are using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters). -`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. - -Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: - -```typescript -import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common'; -import { Observable, throwError } from 'rxjs'; -import { RpcException } from '@nestjs/microservices'; -import * as Sentry from '@sentry/nestjs'; - -@Catch(RpcException) -export class ExceptionFilter implements RpcExceptionFilter { - catch(exception: RpcException, host: ArgumentsHost): Observable { - Sentry.captureException(exception); // optional - return throwError(() => exception.getError()); - } -} -``` - - - - +##### Fine-Tuning Error Capture -If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: - -```javascript {9} -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) - } -} -``` +For more precise control over error reporting in global catch-all exception filters, specific exception filters, or microservices, check out our [CommonJS installation documentation](/platforms/javascript/guides/nestjs/install/commonjs/). diff --git a/platform-includes/getting-started-verify/javascript.nestjs.mdx b/platform-includes/getting-started-verify/javascript.nestjs.mdx index c1bcc15475103..61f83026121e5 100644 --- a/platform-includes/getting-started-verify/javascript.nestjs.mdx +++ b/platform-includes/getting-started-verify/javascript.nestjs.mdx @@ -1,6 +1,44 @@ +### Issues + +First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following route to your application, which will call an undefined function, triggering an error that Sentry will capture: + ```javascript @Get("/debug-sentry") -getError() { - throw new Error("My first Sentry error!"); + getError() { + setTimeout(() => { + try { + foo(); + } catch (e) { + Sentry.captureException(e); + } + }, 99); + } +``` + + +### Tracing + +To test your tracing configuration, update the previous code snippet by starting a performance trace to measure the time it takes for the execution of your code: + +```javascript +@Get("/debug-sentry") + getError() { + Sentry.startSpan( + { + op: "test", + name: "My First Test Transaction", + }, + () => { + setTimeout(() => { + try { + foo(); + } catch (e) { + Sentry.captureException(e); + } + }, 99); + }, + ); } ``` + + From bb448d15f55749d1ecd8a1c10a3a6a24091093d3 Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Mon, 28 Apr 2025 10:18:24 +0200 Subject: [PATCH 2/7] PR feedback --- .../javascript/common/install/commonjs.mdx | 116 +----------------- .../getting-started-use/javascript.nestjs.mdx | 76 ++++++++++-- .../javascript.nestjs.mdx | 12 +- 3 files changed, 71 insertions(+), 133 deletions(-) diff --git a/docs/platforms/javascript/common/install/commonjs.mdx b/docs/platforms/javascript/common/install/commonjs.mdx index f1ae5cd08ebf2..b82ef45c201e0 100644 --- a/docs/platforms/javascript/common/install/commonjs.mdx +++ b/docs/platforms/javascript/common/install/commonjs.mdx @@ -27,118 +27,4 @@ You need to create a file named `instrument.js` that imports and initializes Sen You need to require or import the `instrument.js` file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application: -```javascript {filename: main.ts} -// Import this first! -import "./instrument"; - -// Now import other modules -import { NestFactory } from "@nestjs/core"; -import { AppModule } from "./app.module"; - -async function bootstrap() { - const app = await NestFactory.create(AppModule); - await app.listen(3000); -} - -bootstrap(); -``` - -Afterwards, add the `SentryModule` as a root module to your main module: - -```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 {} -``` - -If you're using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. -This decorator will report all unexpected errors that are received by your global error filter to Sentry: - -```javascript {2, 6} -import { Catch, ExceptionFilter } from '@nestjs/common'; -import { SentryExceptionCaptured } from '@sentry/nestjs'; - -@Catch() -export class YourCatchAllExceptionFilter implements ExceptionFilter { - @SentryExceptionCaptured() - catch(exception, host): void { - // your implementation here - } -} -``` - -By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. -`HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. - -If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. -This filter will report any unhandled errors that aren't caught by other error filters to Sentry. -**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters. - -```javascript {3, 9} -import { Module } from "@nestjs/common"; -import { APP_FILTER } from "@nestjs/core"; -import { SentryGlobalFilter } from "@sentry/nestjs/setup"; - -@Module({ - providers: [ - { - provide: APP_FILTER, - useClass: SentryGlobalFilter, - }, - // ..other providers - ], -}) -export class AppModule {} -``` - - - -If you are using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters). -`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. - -Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: - -```typescript -import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; -import { Observable, throwError } from "rxjs"; -import { RpcException } from "@nestjs/microservices"; -import * as Sentry from "@sentry/nestjs"; - -@Catch(RpcException) -export class ExceptionFilter implements RpcExceptionFilter { - catch(exception: RpcException, host: ArgumentsHost): Observable { - Sentry.captureException(exception); // optional - return throwError(() => exception.getError()); - } -} -``` - - - -If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: - -```javascript {9} -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) - } -} -``` + diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index 65b8e320f2a29..fc3f214fae18b 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -1,4 +1,8 @@ -```javascript {filename: main.ts} + + +You need to require or import the `instrument.js` file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application: + +```javascript {filename: main.ts} {1-2} // Import this first! import "./instrument"; @@ -14,7 +18,7 @@ async function bootstrap() { bootstrap(); ``` -Afterward, add the `SentryModule` as a root module to your main module: +Afterwards, add the `SentryModule` as a root module to your main module: ```javascript {filename: app.module.ts} {2, 8} import { Module } from "@nestjs/common"; @@ -33,17 +37,30 @@ import { AppService } from "./app.service"; export class AppModule {} ``` -By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are not captured automatically as they mostly act as control flow vehicles. +If you're using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. +This decorator will report all unexpected errors that are received by your global error filter to Sentry: -##### Basic Error Capture +```javascript {2, 6} +import { Catch, ExceptionFilter } from '@nestjs/common'; +import { SentryExceptionCaptured } from '@sentry/nestjs'; + +@Catch() +export class YourCatchAllExceptionFilter implements ExceptionFilter { + @SentryExceptionCaptured() + catch(exception, host): void { + // your implementation here + } +} +``` -Add the `SentryGlobalFilter` to the providers of your main module. This filter will report any unhandled errors that aren't caught by other error filters to Sentry. +By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. +`HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. - - Make sure to register `SentryGlobalFilter` before any other exception filters. - +If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. +This filter will report any unhandled errors that aren't caught by other error filters to Sentry. +**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters. -```javascript {3, 9} +```javascript {2-3, 7-10} import { Module } from "@nestjs/common"; import { APP_FILTER } from "@nestjs/core"; import { SentryGlobalFilter } from "@sentry/nestjs/setup"; @@ -60,6 +77,43 @@ import { SentryGlobalFilter } from "@sentry/nestjs/setup"; export class AppModule {} ``` -##### Fine-Tuning Error Capture + + +If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters). +`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. + +Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: + +```typescript +import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; +import { Observable, throwError } from "rxjs"; +import { RpcException } from "@nestjs/microservices"; +import * as Sentry from "@sentry/nestjs"; + +@Catch(RpcException) +export class ExceptionFilter implements RpcExceptionFilter { + catch(exception: RpcException, host: ArgumentsHost): Observable { + Sentry.captureException(exception); // optional + return throwError(() => exception.getError()); + } +} +``` + + -For more precise control over error reporting in global catch-all exception filters, specific exception filters, or microservices, check out our [CommonJS installation documentation](/platforms/javascript/guides/nestjs/install/commonjs/). +If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: + +```javascript {4-9} +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) + } +} +``` diff --git a/platform-includes/getting-started-verify/javascript.nestjs.mdx b/platform-includes/getting-started-verify/javascript.nestjs.mdx index 61f83026121e5..6764db3daa2d1 100644 --- a/platform-includes/getting-started-verify/javascript.nestjs.mdx +++ b/platform-includes/getting-started-verify/javascript.nestjs.mdx @@ -5,13 +5,11 @@ First, let's verify that Sentry captures errors and creates issues in your Sentr ```javascript @Get("/debug-sentry") getError() { - setTimeout(() => { - try { - foo(); - } catch (e) { - Sentry.captureException(e); - } - }, 99); + try { + foo(); + } catch (e) { + Sentry.captureException(e); + } } ``` From 352fc652963cceee435158b51bcb806fd1b7ef1e Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Tue, 6 May 2025 08:32:33 +0200 Subject: [PATCH 3/7] Update platform-includes/getting-started-use/javascript.nestjs.mdx Co-authored-by: Lukas Stracke --- platform-includes/getting-started-use/javascript.nestjs.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index fc3f214fae18b..a1a004148c654 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -103,7 +103,7 @@ export class ExceptionFilter implements RpcExceptionFilter { If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: -```javascript {4-9} +```javascript {4,9} import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; import { BaseExceptionFilter } from '@nestjs/core'; import { ExampleException } from './example.exception'; From bd7fea77606145dc78eec87baa2bd4272dca6c1a Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Tue, 6 May 2025 13:17:44 +0200 Subject: [PATCH 4/7] update guide to TS and create new step for Error Capturing --- .../javascript/guides/nestjs/index.mdx | 158 +++++++++++++++++- .../javascript.nestjs.mdx | 43 +---- .../getting-started-use/javascript.nestjs.mdx | 91 +--------- .../javascript.nestjs.mdx | 14 +- 4 files changed, 165 insertions(+), 141 deletions(-) diff --git a/docs/platforms/javascript/guides/nestjs/index.mdx b/docs/platforms/javascript/guides/nestjs/index.mdx index 4d79a147016fd..57f8a5d720f00 100644 --- a/docs/platforms/javascript/guides/nestjs/index.mdx +++ b/docs/platforms/javascript/guides/nestjs/index.mdx @@ -9,4 +9,160 @@ categories: - server-node --- - + + +## Step 1: Install + +Choose the features you want to configure, and this guide will show you how: + + + + + +### Install the Sentry SDK + +Run the command for your preferred package manager to add the Sentry SDK to your application: + + + +## Step 2: Configure + +### Initialize the Sentry SDK + +To import and initialize Sentry, create a file named `instrument.ts` in the root directory of your project and add the following code: + + + +### Apply Instrumentation to Your App + + + +## Step 3: Capture Nest.js Errors + +By default, Sentry only captures unhandled exceptions that aren't caught by an error filter. +Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) aren't captured by default because they mostly act as control flow vehicles. + +To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions: + +### Using a Global Catch-All Exception Filter + +If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method: + +```typescript {2, 6} +import { Catch, ExceptionFilter } from "@nestjs/common"; +import { SentryExceptionCaptured } from "@sentry/nestjs"; + +@Catch() +export class YourCatchAllExceptionFilter implements ExceptionFilter { + @SentryExceptionCaptured() + catch(exception, host): void { + // your implementation here + } +} +``` + +### Not Using a Global Catch-All Exception Filter + +If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters: + +```typescript {2-3, 7-10} +import { Module } from "@nestjs/common"; +import { APP_FILTER } from "@nestjs/core"; +import { SentryGlobalFilter } from "@sentry/nestjs/setup"; + +@Module({ + providers: [ + { + provide: APP_FILTER, + useClass: SentryGlobalFilter, + }, + // ..other providers + ], +}) +export class AppModule {} +``` + +### Using Error Filters for Specific Exception Types + +If you have error filters for specific types of exceptions (for example, `@Catch(HttpException)`) and you want to report these errors to Sentry, you need to capture them in the `catch()` handler using `Sentry.captureException()`: + +```typescript {4,9} +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); + } +} +``` + + +If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see [Nest.js Microservices documentation](https://docs.nestjs.com/microservices/exception-filters)). +`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. + +Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: + +```typescript +import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; +import { Observable, throwError } from "rxjs"; +import { RpcException } from "@nestjs/microservices"; +import * as Sentry from "@sentry/nestjs"; + +@Catch(RpcException) +export class ExceptionFilter implements RpcExceptionFilter { + catch(exception: RpcException, host: ArgumentsHost): Observable { + Sentry.captureException(exception); // optional + return throwError(() => exception.getError()); + } +} +``` + + + +## Step 4: Add Readable Stack Traces With Source Maps (Optional) + +The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry. +The easiest way to do this is by using the Sentry Wizard: + +```bash +npx @sentry/wizard@latest -i sourcemaps +``` + +## Step 5: Verify Your Setup + +Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project. + + + +### View Captured Data in Sentry + +Now, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear). + + + +## Next Steps + +At this point, you should have integrated Sentry into your Nest.js application and should already be sending data to your Sentry project. + +Now's a good time to customize your setup and look into more advanced topics. +Our next recommended steps for you are: + +- Extend Sentry to your frontend using one of our [frontend SDKs](/) +- Learn how to [manually capture errors](/platforms/javascript/guides/nestjs/usage/) +- Continue to [customize your configuration](/platforms/javascript/guides/nestjs/configuration/) +- Get familiar with [Sentry's product features](/product) like tracing, insights, and alerts + + + +- Check out alternative installation methods +- Find various topics in Troubleshooting +- [Get support](https://sentry.zendesk.com/hc/en-us/) + + diff --git a/platform-includes/getting-started-config/javascript.nestjs.mdx b/platform-includes/getting-started-config/javascript.nestjs.mdx index f927dbf398f41..0ed1b389b40f3 100644 --- a/platform-includes/getting-started-config/javascript.nestjs.mdx +++ b/platform-includes/getting-started-config/javascript.nestjs.mdx @@ -1,45 +1,4 @@ -```javascript {tabTitle:ESM} {filename: instrument.mjs} -import * as Sentry from "@sentry/nestjs"; -// ___PRODUCT_OPTION_START___ profiling -import { nodeProfilingIntegration } from "@sentry/profiling-node"; -// ___PRODUCT_OPTION_END___ profiling - -// Ensure to call this before importing any other modules! -Sentry.init({ - dsn: "___PUBLIC_DSN___", - - // Adds request headers and IP for users, for more info visit: - // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#sendDefaultPii - sendDefaultPii: true, - - // ___PRODUCT_OPTION_START___ profiling - integrations: [ - // Add our Profiling integration - nodeProfilingIntegration(), - ], - // ___PRODUCT_OPTION_END___ profiling - // ___PRODUCT_OPTION_START___ performance - - // Set tracesSampleRate to 1.0 to capture 100% - // of transactions for tracing. - // We recommend adjusting this value in production - // Learn more at - // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate - tracesSampleRate: 1.0, - // ___PRODUCT_OPTION_END___ performance - // ___PRODUCT_OPTION_START___ profiling - - // Set profilesSampleRate to 1.0 to profile 100% - // of sampled transactions. - // This is relative to tracesSampleRate - // Learn more at - // https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate - profilesSampleRate: 1.0, - // ___PRODUCT_OPTION_END___ profiling -}); -``` - -```javascript {tabTitle:CommonJS} {filename: instrument.js} +```typescript {filename: instrument.ts} const Sentry = require("@sentry/nestjs"); // ___PRODUCT_OPTION_START___ profiling const { nodeProfilingIntegration } = require("@sentry/profiling-node"); diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index a1a004148c654..941dfcc656a69 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -1,8 +1,6 @@ - +Make sure to import the `instrument.ts` file before any other modules: -You need to require or import the `instrument.js` file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application: - -```javascript {filename: main.ts} {1-2} +```typescript {filename: main.ts} {1-2} // Import this first! import "./instrument"; @@ -18,9 +16,9 @@ async function bootstrap() { bootstrap(); ``` -Afterwards, add the `SentryModule` as a root module to your main module: +Afterward, add the `SentryModule` as a root module to your main module: -```javascript {filename: app.module.ts} {2, 8} +```typescript {filename: app.module.ts} {2, 8} import { Module } from "@nestjs/common"; import { SentryModule } from "@sentry/nestjs/setup"; import { AppController } from "./app.controller"; @@ -36,84 +34,3 @@ import { AppService } from "./app.service"; }) export class AppModule {} ``` - -If you're using a global catch-all exception filter (which is either a filter registered with `app.useGlobalFilters()` or a filter registered in your app module providers annotated with a `@Catch()` decorator without arguments), add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. -This decorator will report all unexpected errors that are received by your global error filter to Sentry: - -```javascript {2, 6} -import { Catch, ExceptionFilter } from '@nestjs/common'; -import { SentryExceptionCaptured } from '@sentry/nestjs'; - -@Catch() -export class YourCatchAllExceptionFilter implements ExceptionFilter { - @SentryExceptionCaptured() - catch(exception, host): void { - // your implementation here - } -} -``` - -By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. -`HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles. - -If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. -This filter will report any unhandled errors that aren't caught by other error filters to Sentry. -**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters. - -```javascript {2-3, 7-10} -import { Module } from "@nestjs/common"; -import { APP_FILTER } from "@nestjs/core"; -import { SentryGlobalFilter } from "@sentry/nestjs/setup"; - -@Module({ - providers: [ - { - provide: APP_FILTER, - useClass: SentryGlobalFilter, - }, - // ..other providers - ], -}) -export class AppModule {} -``` - - - -If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters). -`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. - -Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: - -```typescript -import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; -import { Observable, throwError } from "rxjs"; -import { RpcException } from "@nestjs/microservices"; -import * as Sentry from "@sentry/nestjs"; - -@Catch(RpcException) -export class ExceptionFilter implements RpcExceptionFilter { - catch(exception: RpcException, host: ArgumentsHost): Observable { - Sentry.captureException(exception); // optional - return throwError(() => exception.getError()); - } -} -``` - - - -If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`: - -```javascript {4,9} -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) - } -} -``` diff --git a/platform-includes/getting-started-verify/javascript.nestjs.mdx b/platform-includes/getting-started-verify/javascript.nestjs.mdx index 6764db3daa2d1..93618d7e8c77a 100644 --- a/platform-includes/getting-started-verify/javascript.nestjs.mdx +++ b/platform-includes/getting-started-verify/javascript.nestjs.mdx @@ -1,15 +1,11 @@ ### Issues -First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following route to your application, which will call an undefined function, triggering an error that Sentry will capture: +First, let's verify that Sentry captures errors and creates issues in your Sentry project. Add the following route to your application, which will trigger an error that Sentry will capture: ```javascript @Get("/debug-sentry") getError() { - try { - foo(); - } catch (e) { - Sentry.captureException(e); - } + throw new Error("My first Sentry error!"); } ``` @@ -28,11 +24,7 @@ To test your tracing configuration, update the previous code snippet by starting }, () => { setTimeout(() => { - try { - foo(); - } catch (e) { - Sentry.captureException(e); - } + throw new Error("My first Sentry error!"); }, 99); }, ); From 17472be5a61b90327bc2366add359e51ffdef79d Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Tue, 6 May 2025 13:49:51 +0200 Subject: [PATCH 5/7] create and use new platform include for capturing error content --- .../javascript/guides/nestjs/index.mdx | 85 +------------------ .../javascript.nestjs.mdx | 84 ++++++++++++++++++ .../getting-started-use/javascript.nestjs.mdx | 2 + 3 files changed, 87 insertions(+), 84 deletions(-) create mode 100644 platform-includes/getting-started-capture-errors/javascript.nestjs.mdx diff --git a/docs/platforms/javascript/guides/nestjs/index.mdx b/docs/platforms/javascript/guides/nestjs/index.mdx index 57f8a5d720f00..ecea0b9231900 100644 --- a/docs/platforms/javascript/guides/nestjs/index.mdx +++ b/docs/platforms/javascript/guides/nestjs/index.mdx @@ -41,90 +41,7 @@ To import and initialize Sentry, create a file named `instrument.ts` in the root ## Step 3: Capture Nest.js Errors -By default, Sentry only captures unhandled exceptions that aren't caught by an error filter. -Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) aren't captured by default because they mostly act as control flow vehicles. - -To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions: - -### Using a Global Catch-All Exception Filter - -If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method: - -```typescript {2, 6} -import { Catch, ExceptionFilter } from "@nestjs/common"; -import { SentryExceptionCaptured } from "@sentry/nestjs"; - -@Catch() -export class YourCatchAllExceptionFilter implements ExceptionFilter { - @SentryExceptionCaptured() - catch(exception, host): void { - // your implementation here - } -} -``` - -### Not Using a Global Catch-All Exception Filter - -If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters: - -```typescript {2-3, 7-10} -import { Module } from "@nestjs/common"; -import { APP_FILTER } from "@nestjs/core"; -import { SentryGlobalFilter } from "@sentry/nestjs/setup"; - -@Module({ - providers: [ - { - provide: APP_FILTER, - useClass: SentryGlobalFilter, - }, - // ..other providers - ], -}) -export class AppModule {} -``` - -### Using Error Filters for Specific Exception Types - -If you have error filters for specific types of exceptions (for example, `@Catch(HttpException)`) and you want to report these errors to Sentry, you need to capture them in the `catch()` handler using `Sentry.captureException()`: - -```typescript {4,9} -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); - } -} -``` - - -If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see [Nest.js Microservices documentation](https://docs.nestjs.com/microservices/exception-filters)). -`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. - -Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: - -```typescript -import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; -import { Observable, throwError } from "rxjs"; -import { RpcException } from "@nestjs/microservices"; -import * as Sentry from "@sentry/nestjs"; - -@Catch(RpcException) -export class ExceptionFilter implements RpcExceptionFilter { - catch(exception: RpcException, host: ArgumentsHost): Observable { - Sentry.captureException(exception); // optional - return throwError(() => exception.getError()); - } -} -``` - - + ## Step 4: Add Readable Stack Traces With Source Maps (Optional) diff --git a/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx b/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx new file mode 100644 index 0000000000000..1e0fe4bb8cc9b --- /dev/null +++ b/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx @@ -0,0 +1,84 @@ +By default, Sentry only captures unhandled exceptions that aren't caught by an error filter. +Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) aren't captured by default because they mostly act as control flow vehicles. + +To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions: + +### Using a Global Catch-All Exception Filter + +If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method: + +```typescript {2, 6} +import { Catch, ExceptionFilter } from "@nestjs/common"; +import { SentryExceptionCaptured } from "@sentry/nestjs"; + +@Catch() +export class YourCatchAllExceptionFilter implements ExceptionFilter { + @SentryExceptionCaptured() + catch(exception, host): void { + // your implementation here + } +} +``` + +### Not Using a Global Catch-All Exception Filter + +If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters: + +```typescript {2-3, 7-10} +import { Module } from "@nestjs/common"; +import { APP_FILTER } from "@nestjs/core"; +import { SentryGlobalFilter } from "@sentry/nestjs/setup"; + +@Module({ + providers: [ + { + provide: APP_FILTER, + useClass: SentryGlobalFilter, + }, + // ..other providers + ], +}) +export class AppModule {} +``` + +### Using Error Filters for Specific Exception Types + +If you have error filters for specific types of exceptions (for example, `@Catch(HttpException)`) and you want to report these errors to Sentry, you need to capture them in the `catch()` handler using `Sentry.captureException()`: + +```typescript {4,9} +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); + } +} +``` + + +If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see [Nest.js Microservices documentation](https://docs.nestjs.com/microservices/exception-filters)). +`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`. + +Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry: + +```typescript +import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common"; +import { Observable, throwError } from "rxjs"; +import { RpcException } from "@nestjs/microservices"; +import * as Sentry from "@sentry/nestjs"; + +@Catch(RpcException) +export class ExceptionFilter implements RpcExceptionFilter { + catch(exception: RpcException, host: ArgumentsHost): Observable { + Sentry.captureException(exception); // optional + return throwError(() => exception.getError()); + } +} +``` + + diff --git a/platform-includes/getting-started-use/javascript.nestjs.mdx b/platform-includes/getting-started-use/javascript.nestjs.mdx index 941dfcc656a69..fa2080009b46e 100644 --- a/platform-includes/getting-started-use/javascript.nestjs.mdx +++ b/platform-includes/getting-started-use/javascript.nestjs.mdx @@ -34,3 +34,5 @@ import { AppService } from "./app.service"; }) export class AppModule {} ``` + + From d44389548b5e47df325224a705a21c75591d269f Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Thu, 8 May 2025 08:18:42 +0200 Subject: [PATCH 6/7] pr feedback - rewrite subheadings --- .../getting-started-capture-errors/javascript.nestjs.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx b/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx index 1e0fe4bb8cc9b..ba28fe3fce957 100644 --- a/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx +++ b/platform-includes/getting-started-capture-errors/javascript.nestjs.mdx @@ -3,7 +3,7 @@ Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/ To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions: -### Using a Global Catch-All Exception Filter +### Using a Custom Global Filter If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method: @@ -20,7 +20,7 @@ export class YourCatchAllExceptionFilter implements ExceptionFilter { } ``` -### Not Using a Global Catch-All Exception Filter +### Not Using a Custom Global Filter If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters: From 6e33498af41d4e20b92a7ee0f5160af3a3f4709a Mon Sep 17 00:00:00 2001 From: Sarah Mischinger Date: Thu, 15 May 2025 10:08:39 +0200 Subject: [PATCH 7/7] remove duplicate includes --- .../javascript/guides/nestjs/index.mdx | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/platforms/javascript/guides/nestjs/index.mdx b/docs/platforms/javascript/guides/nestjs/index.mdx index ecea0b9231900..f3492b692060f 100644 --- a/docs/platforms/javascript/guides/nestjs/index.mdx +++ b/docs/platforms/javascript/guides/nestjs/index.mdx @@ -37,7 +37,42 @@ To import and initialize Sentry, create a file named `instrument.ts` in the root ### Apply Instrumentation to Your App - +Make sure to import the `instrument.ts` file before any other modules: + +```typescript {filename: main.ts} {1-2} +// Import this first! +import "./instrument"; + +// Now import other modules +import { NestFactory } from "@nestjs/core"; +import { AppModule } from "./app.module"; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + await app.listen(3000); +} + +bootstrap(); +``` + +Afterward, add the `SentryModule` as a root module to your main module: + +```typescript {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 {} +``` ## Step 3: Capture Nest.js Errors