Skip to content

Commit 2c6b6a3

Browse files
inventarSarahLms24
andauthored
docs(js): Review and update Nest.js Quick Start guide (#13497)
<!-- Use this checklist to make sure your PR is ready for merge. You may delete any sections you don't need. --> ## DESCRIBE YOUR PR The Nest.js quick start guide already looked great -> I added and removed some content to be more consistent with our other guides. I tried to streamline the CommonJS section, as we already have the details on [this page](https://docs.sentry.io/platforms/javascript/guides/nestjs/install/commonjs/) (I added a link to the page). Plus, the content seemed too detailed for a Quick Start guide IMO. Naturally, if you say the info I removed is vital for the QS guide, we can add it back in. Additionally, I expanded the "Verify Your Setup" section so the example provided is like our [Node.js one](https://docs.sentry.io/platforms/javascript/guides/node/#step-4-verify-your-setup). Closes: #13498 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
1 parent 5bdc7ea commit 2c6b6a3

File tree

6 files changed

+239
-129
lines changed

6 files changed

+239
-129
lines changed
Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Nest.js
3-
description: "Learn about using Sentry with Nest.js."
3+
description: "Learn how to set up Sentry in your Nest.js app and capture your first errors."
44
sdk: sentry.javascript.nestjs
55
fallbackGuide: javascript.node
66
categories:
@@ -9,6 +9,112 @@ categories:
99
- server-node
1010
---
1111

12-
This guide explains how to set up Sentry in your Nest.js application.
12+
<PlatformContent includePath="getting-started-prerequisites" />
1313

14-
<PlatformContent includePath="getting-started-node" />
14+
## Step 1: Install
15+
16+
Choose the features you want to configure, and this guide will show you how:
17+
18+
<OnboardingOptionButtons
19+
options={["error-monitoring", "performance", "profiling"]}
20+
/>
21+
22+
<PlatformContent includePath="getting-started-features-expandable" />
23+
24+
### Install the Sentry SDK
25+
26+
Run the command for your preferred package manager to add the Sentry SDK to your application:
27+
28+
<PlatformContent includePath="getting-started-install" />
29+
30+
## Step 2: Configure
31+
32+
### Initialize the Sentry SDK
33+
34+
To import and initialize Sentry, create a file named `instrument.ts` in the root directory of your project and add the following code:
35+
36+
<PlatformContent includePath="getting-started-config" />
37+
38+
### Apply Instrumentation to Your App
39+
40+
Make sure to import the `instrument.ts` file before any other modules:
41+
42+
```typescript {filename: main.ts} {1-2}
43+
// Import this first!
44+
import "./instrument";
45+
46+
// Now import other modules
47+
import { NestFactory } from "@nestjs/core";
48+
import { AppModule } from "./app.module";
49+
50+
async function bootstrap() {
51+
const app = await NestFactory.create(AppModule);
52+
await app.listen(3000);
53+
}
54+
55+
bootstrap();
56+
```
57+
58+
Afterward, add the `SentryModule` as a root module to your main module:
59+
60+
```typescript {filename: app.module.ts} {2, 8}
61+
import { Module } from "@nestjs/common";
62+
import { SentryModule } from "@sentry/nestjs/setup";
63+
import { AppController } from "./app.controller";
64+
import { AppService } from "./app.service";
65+
66+
@Module({
67+
imports: [
68+
SentryModule.forRoot(),
69+
// ...other modules
70+
],
71+
controllers: [AppController],
72+
providers: [AppService],
73+
})
74+
export class AppModule {}
75+
```
76+
77+
## Step 3: Capture Nest.js Errors
78+
79+
<PlatformContent includePath="getting-started-capture-errors" />
80+
81+
## Step 4: Add Readable Stack Traces With Source Maps (Optional)
82+
83+
The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry.
84+
The easiest way to do this is by using the Sentry Wizard:
85+
86+
```bash
87+
npx @sentry/wizard@latest -i sourcemaps
88+
```
89+
90+
## Step 5: Verify Your Setup
91+
92+
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
93+
94+
<PlatformContent includePath="getting-started-verify" />
95+
96+
### View Captured Data in Sentry
97+
98+
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).
99+
100+
<PlatformContent includePath="getting-started-verify-locate-data" />
101+
102+
## Next Steps
103+
104+
At this point, you should have integrated Sentry into your Nest.js application and should already be sending data to your Sentry project.
105+
106+
Now's a good time to customize your setup and look into more advanced topics.
107+
Our next recommended steps for you are:
108+
109+
- Extend Sentry to your frontend using one of our [frontend SDKs](/)
110+
- Learn how to [manually capture errors](/platforms/javascript/guides/nestjs/usage/)
111+
- Continue to [customize your configuration](/platforms/javascript/guides/nestjs/configuration/)
112+
- Get familiar with [Sentry's product features](/product) like tracing, insights, and alerts
113+
114+
<Expandable permalink={false} title="Are you having problems setting up the SDK?">
115+
116+
- Check out alternative <PlatformLink to="/install">installation methods</PlatformLink>
117+
- Find various topics in <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
118+
- [Get support](https://sentry.zendesk.com/hc/en-us/)
119+
120+
</Expandable>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
By default, Sentry only captures unhandled exceptions that aren't caught by an error filter.
2+
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.
3+
4+
To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions:
5+
6+
### Using a Custom Global Filter
7+
8+
If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method:
9+
10+
```typescript {2, 6}
11+
import { Catch, ExceptionFilter } from "@nestjs/common";
12+
import { SentryExceptionCaptured } from "@sentry/nestjs";
13+
14+
@Catch()
15+
export class YourCatchAllExceptionFilter implements ExceptionFilter {
16+
@SentryExceptionCaptured()
17+
catch(exception, host): void {
18+
// your implementation here
19+
}
20+
}
21+
```
22+
23+
### Not Using a Custom Global Filter
24+
25+
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:
26+
27+
```typescript {2-3, 7-10}
28+
import { Module } from "@nestjs/common";
29+
import { APP_FILTER } from "@nestjs/core";
30+
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
31+
32+
@Module({
33+
providers: [
34+
{
35+
provide: APP_FILTER,
36+
useClass: SentryGlobalFilter,
37+
},
38+
// ..other providers
39+
],
40+
})
41+
export class AppModule {}
42+
```
43+
44+
### Using Error Filters for Specific Exception Types
45+
46+
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()`:
47+
48+
```typescript {4,9}
49+
import { ArgumentsHost, BadRequestException, Catch } from "@nestjs/common";
50+
import { BaseExceptionFilter } from "@nestjs/core";
51+
import { ExampleException } from "./example.exception";
52+
import * as Sentry from "@sentry/nestjs";
53+
54+
@Catch(ExampleException)
55+
export class ExampleExceptionFilter extends BaseExceptionFilter {
56+
catch(exception: unknown, host: ArgumentsHost) {
57+
Sentry.captureException(exception);
58+
return super.catch(new BadRequestException(exception.message), host);
59+
}
60+
}
61+
```
62+
63+
<Expandable title="Are you using Microservices?">
64+
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)).
65+
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
66+
67+
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
68+
69+
```typescript
70+
import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common";
71+
import { Observable, throwError } from "rxjs";
72+
import { RpcException } from "@nestjs/microservices";
73+
import * as Sentry from "@sentry/nestjs";
74+
75+
@Catch(RpcException)
76+
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
77+
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
78+
Sentry.captureException(exception); // optional
79+
return throwError(() => exception.getError());
80+
}
81+
}
82+
```
83+
84+
</Expandable>
Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,4 @@
1-
```javascript {tabTitle:ESM} {filename: instrument.mjs}
2-
import * as Sentry from "@sentry/nestjs";
3-
// ___PRODUCT_OPTION_START___ profiling
4-
import { nodeProfilingIntegration } from '@sentry/profiling-node';
5-
// ___PRODUCT_OPTION_END___ profiling
6-
7-
// Ensure to call this before importing any other modules!
8-
Sentry.init({
9-
dsn: "___PUBLIC_DSN___",
10-
11-
// Adds request headers and IP for users, for more info visit:
12-
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#sendDefaultPii
13-
sendDefaultPii: true,
14-
15-
// ___PRODUCT_OPTION_START___ profiling
16-
integrations: [
17-
// Add our Profiling integration
18-
nodeProfilingIntegration(),
19-
],
20-
// ___PRODUCT_OPTION_END___ profiling
21-
// ___PRODUCT_OPTION_START___ performance
22-
23-
// Add Tracing by setting tracesSampleRate
24-
// We recommend adjusting this value in production
25-
tracesSampleRate: 1.0,
26-
// ___PRODUCT_OPTION_END___ performance
27-
// ___PRODUCT_OPTION_START___ profiling
28-
29-
// Set sampling rate for profiling
30-
// This is relative to tracesSampleRate
31-
profilesSampleRate: 1.0,
32-
// ___PRODUCT_OPTION_END___ profiling
33-
});
34-
```
35-
36-
```javascript {tabTitle:CommonJS} {filename: instrument.js}
1+
```typescript {filename: instrument.ts}
372
const Sentry = require("@sentry/nestjs");
383
// ___PRODUCT_OPTION_START___ profiling
394
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
@@ -50,16 +15,21 @@ Sentry.init({
5015
// ___PRODUCT_OPTION_END___ profiling
5116
// ___PRODUCT_OPTION_START___ performance
5217

53-
// Add Tracing by setting tracesSampleRate
18+
// Set tracesSampleRate to 1.0 to capture 100%
19+
// of transactions for tracing.
5420
// We recommend adjusting this value in production
21+
// Learn more at
22+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate
5523
tracesSampleRate: 1.0,
5624
// ___PRODUCT_OPTION_END___ performance
5725
// ___PRODUCT_OPTION_START___ profiling
5826

59-
// Set sampling rate for profiling
27+
// Set profilesSampleRate to 1.0 to profile 100%
28+
// of sampled transactions.
6029
// This is relative to tracesSampleRate
30+
// Learn more at
31+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate
6132
profilesSampleRate: 1.0,
6233
// ___PRODUCT_OPTION_END___ profiling
6334
});
6435
```
65-

platform-includes/getting-started-node/javascript.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Our next recommended steps for you are:
8585
<Expandable permalink={false} title="Are you having problems setting up the SDK?">
8686

8787
- Check out alternative <PlatformLink to="/install">installation methods</PlatformLink>
88+
- Find various topics in <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
8889
- [Get support](https://sentry.zendesk.com/hc/en-us/)
8990

9091
</Expandable>
Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
```javascript {filename: main.ts}
1+
Make sure to import the `instrument.ts` file before any other modules:
2+
3+
```typescript {filename: main.ts} {1-2}
24
// Import this first!
35
import "./instrument";
46

@@ -14,9 +16,9 @@ async function bootstrap() {
1416
bootstrap();
1517
```
1618

17-
Afterwards, add the `SentryModule` as a root module to your main module:
19+
Afterward, add the `SentryModule` as a root module to your main module:
1820

19-
```javascript {filename: app.module.ts} {2, 8}
21+
```typescript {filename: app.module.ts} {2, 8}
2022
import { Module } from "@nestjs/common";
2123
import { SentryModule } from "@sentry/nestjs/setup";
2224
import { AppController } from "./app.controller";
@@ -33,85 +35,4 @@ import { AppService } from "./app.service";
3335
export class AppModule {}
3436
```
3537

36-
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.
37-
This decorator will report all unexpected errors that are received by your global error filter to Sentry:
38-
39-
```javascript {2, 6}
40-
import { Catch, ExceptionFilter } from '@nestjs/common';
41-
import { SentryExceptionCaptured } from '@sentry/nestjs';
42-
43-
@Catch()
44-
export class YourCatchAllExceptionFilter implements ExceptionFilter {
45-
@SentryExceptionCaptured()
46-
catch(exception, host): void {
47-
// your implementation here
48-
}
49-
}
50-
```
51-
52-
By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry.
53-
`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.
54-
55-
If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module.
56-
This filter will report any unhandled errors that aren't caught by other error filters to Sentry.
57-
**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters.
58-
59-
```javascript {3, 9}
60-
import { Module } from "@nestjs/common";
61-
import { APP_FILTER } from "@nestjs/core";
62-
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
63-
64-
@Module({
65-
providers: [
66-
{
67-
provide: APP_FILTER,
68-
useClass: SentryGlobalFilter,
69-
},
70-
// ..other providers
71-
],
72-
})
73-
export class AppModule {}
74-
```
75-
76-
<Expandable title="Using Microservices?">
77-
78-
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).
79-
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
80-
81-
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
82-
83-
```typescript
84-
import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common';
85-
import { Observable, throwError } from 'rxjs';
86-
import { RpcException } from '@nestjs/microservices';
87-
import * as Sentry from '@sentry/nestjs';
88-
89-
@Catch(RpcException)
90-
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
91-
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
92-
Sentry.captureException(exception); // optional
93-
return throwError(() => exception.getError());
94-
}
95-
}
96-
```
97-
98-
99-
</Expandable>
100-
101-
102-
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()`:
103-
104-
```javascript {9}
105-
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
106-
import { BaseExceptionFilter } from '@nestjs/core';
107-
import { ExampleException } from './example.exception';
108-
import * as Sentry from '@sentry/nestjs';
109-
110-
@Catch(ExampleException)
111-
export class ExampleExceptionFilter extends BaseExceptionFilter {
112-
catch(exception: unknown, host: ArgumentsHost) {
113-
Sentry.captureException(exception);
114-
return super.catch(new BadRequestException(exception.message), host)
115-
}
116-
}
117-
```
38+
<PlatformContent includePath="getting-started-capture-errors" />

0 commit comments

Comments
 (0)