Skip to content

Commit 5427ba1

Browse files
committed
review and update Nest.js Quick Start guide
1 parent 1cb6bf6 commit 5427ba1

File tree

6 files changed

+185
-75
lines changed

6 files changed

+185
-75
lines changed

docs/platforms/javascript/common/install/commonjs.mdx

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,118 @@ You need to create a file named `instrument.js` that imports and initializes Sen
2727

2828
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:
2929

30-
<PlatformContent includePath="getting-started-use" />
30+
```javascript {filename: main.ts}
31+
// Import this first!
32+
import "./instrument";
33+
34+
// Now import other modules
35+
import { NestFactory } from "@nestjs/core";
36+
import { AppModule } from "./app.module";
37+
38+
async function bootstrap() {
39+
const app = await NestFactory.create(AppModule);
40+
await app.listen(3000);
41+
}
42+
43+
bootstrap();
44+
```
45+
46+
Afterwards, add the `SentryModule` as a root module to your main module:
47+
48+
```javascript {filename: app.module.ts} {2, 8}
49+
import { Module } from "@nestjs/common";
50+
import { SentryModule } from "@sentry/nestjs/setup";
51+
import { AppController } from "./app.controller";
52+
import { AppService } from "./app.service";
53+
54+
@Module({
55+
imports: [
56+
SentryModule.forRoot(),
57+
// ...other modules
58+
],
59+
controllers: [AppController],
60+
providers: [AppService],
61+
})
62+
export class AppModule {}
63+
```
64+
65+
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.
66+
This decorator will report all unexpected errors that are received by your global error filter to Sentry:
67+
68+
```javascript {2, 6}
69+
import { Catch, ExceptionFilter } from '@nestjs/common';
70+
import { SentryExceptionCaptured } from '@sentry/nestjs';
71+
72+
@Catch()
73+
export class YourCatchAllExceptionFilter implements ExceptionFilter {
74+
@SentryExceptionCaptured()
75+
catch(exception, host): void {
76+
// your implementation here
77+
}
78+
}
79+
```
80+
81+
By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry.
82+
`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.
83+
84+
If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module.
85+
This filter will report any unhandled errors that aren't caught by other error filters to Sentry.
86+
**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters.
87+
88+
```javascript {3, 9}
89+
import { Module } from "@nestjs/common";
90+
import { APP_FILTER } from "@nestjs/core";
91+
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
92+
93+
@Module({
94+
providers: [
95+
{
96+
provide: APP_FILTER,
97+
useClass: SentryGlobalFilter,
98+
},
99+
// ..other providers
100+
],
101+
})
102+
export class AppModule {}
103+
```
104+
105+
<Expandable title="Using Microservices?">
106+
107+
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).
108+
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
109+
110+
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
111+
112+
```typescript
113+
import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common";
114+
import { Observable, throwError } from "rxjs";
115+
import { RpcException } from "@nestjs/microservices";
116+
import * as Sentry from "@sentry/nestjs";
117+
118+
@Catch(RpcException)
119+
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
120+
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
121+
Sentry.captureException(exception); // optional
122+
return throwError(() => exception.getError());
123+
}
124+
}
125+
```
126+
127+
</Expandable>
128+
129+
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()`:
130+
131+
```javascript {9}
132+
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
133+
import { BaseExceptionFilter } from '@nestjs/core';
134+
import { ExampleException } from './example.exception';
135+
import * as Sentry from '@sentry/nestjs';
136+
137+
@Catch(ExampleException)
138+
export class ExampleExceptionFilter extends BaseExceptionFilter {
139+
catch(exception: unknown, host: ArgumentsHost) {
140+
Sentry.captureException(exception);
141+
return super.catch(new BadRequestException(exception.message), host)
142+
}
143+
}
144+
```
Lines changed: 1 addition & 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,4 @@ categories:
99
- server-node
1010
---
1111

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

platform-includes/getting-started-config/javascript.nestjs.mdx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
```javascript {tabTitle:ESM} {filename: instrument.mjs}
22
import * as Sentry from "@sentry/nestjs";
33
// ___PRODUCT_OPTION_START___ profiling
4-
import { nodeProfilingIntegration } from '@sentry/profiling-node';
4+
import { nodeProfilingIntegration } from "@sentry/profiling-node";
55
// ___PRODUCT_OPTION_END___ profiling
66

77
// Ensure to call this before importing any other modules!
88
Sentry.init({
99
dsn: "___PUBLIC_DSN___",
10-
10+
1111
// Adds request headers and IP for users, for more info visit:
1212
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#sendDefaultPii
1313
sendDefaultPii: true,
14-
14+
1515
// ___PRODUCT_OPTION_START___ profiling
1616
integrations: [
1717
// Add our Profiling integration
@@ -20,14 +20,20 @@ Sentry.init({
2020
// ___PRODUCT_OPTION_END___ profiling
2121
// ___PRODUCT_OPTION_START___ performance
2222

23-
// Add Tracing by setting tracesSampleRate
23+
// Set tracesSampleRate to 1.0 to capture 100%
24+
// of transactions for tracing.
2425
// We recommend adjusting this value in production
26+
// Learn more at
27+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate
2528
tracesSampleRate: 1.0,
2629
// ___PRODUCT_OPTION_END___ performance
2730
// ___PRODUCT_OPTION_START___ profiling
2831

29-
// Set sampling rate for profiling
32+
// Set profilesSampleRate to 1.0 to profile 100%
33+
// of sampled transactions.
3034
// This is relative to tracesSampleRate
35+
// Learn more at
36+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate
3137
profilesSampleRate: 1.0,
3238
// ___PRODUCT_OPTION_END___ profiling
3339
});
@@ -50,16 +56,21 @@ Sentry.init({
5056
// ___PRODUCT_OPTION_END___ profiling
5157
// ___PRODUCT_OPTION_START___ performance
5258

53-
// Add Tracing by setting tracesSampleRate
59+
// Set tracesSampleRate to 1.0 to capture 100%
60+
// of transactions for tracing.
5461
// We recommend adjusting this value in production
62+
// Learn more at
63+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#tracesSampleRate
5564
tracesSampleRate: 1.0,
5665
// ___PRODUCT_OPTION_END___ performance
5766
// ___PRODUCT_OPTION_START___ profiling
5867

59-
// Set sampling rate for profiling
68+
// Set profilesSampleRate to 1.0 to profile 100%
69+
// of sampled transactions.
6070
// This is relative to tracesSampleRate
71+
// Learn more at
72+
// https://docs.sentry.io/platforms/javascript/guides/nestjs/configuration/options/#profilesSampleRate
6173
profilesSampleRate: 1.0,
6274
// ___PRODUCT_OPTION_END___ profiling
6375
});
6476
```
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>

platform-includes/getting-started-use/javascript.nestjs.mdx

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function bootstrap() {
1414
bootstrap();
1515
```
1616

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

1919
```javascript {filename: app.module.ts} {2, 8}
2020
import { Module } from "@nestjs/common";
@@ -33,28 +33,15 @@ import { AppService } from "./app.service";
3333
export class AppModule {}
3434
```
3535

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:
36+
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.
3837

39-
```javascript {2, 6}
40-
import { Catch, ExceptionFilter } from '@nestjs/common';
41-
import { SentryExceptionCaptured } from '@sentry/nestjs';
38+
##### Basic Error Capture
4239

43-
@Catch()
44-
export class YourCatchAllExceptionFilter implements ExceptionFilter {
45-
@SentryExceptionCaptured()
46-
catch(exception, host): void {
47-
// your implementation here
48-
}
49-
}
50-
```
40+
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.
5141

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.
42+
<Alert level="warning" title="Important">
43+
Make sure to register `SentryGlobalFilter` before any other exception filters.
44+
</Alert>
5845

5946
```javascript {3, 9}
6047
import { Module } from "@nestjs/common";
@@ -73,45 +60,6 @@ import { SentryGlobalFilter } from "@sentry/nestjs/setup";
7360
export class AppModule {}
7461
```
7562

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-
63+
##### Fine-Tuning Error Capture
10164

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-
```
65+
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/).
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
1+
### Issues
2+
3+
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:
4+
15
```javascript
26
@Get("/debug-sentry")
3-
getError() {
4-
throw new Error("My first Sentry error!");
7+
getError() {
8+
setTimeout(() => {
9+
try {
10+
foo();
11+
} catch (e) {
12+
Sentry.captureException(e);
13+
}
14+
}, 99);
15+
}
16+
```
17+
18+
<OnboardingOption optionId="performance">
19+
### Tracing
20+
21+
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:
22+
23+
```javascript
24+
@Get("/debug-sentry")
25+
getError() {
26+
Sentry.startSpan(
27+
{
28+
op: "test",
29+
name: "My First Test Transaction",
30+
},
31+
() => {
32+
setTimeout(() => {
33+
try {
34+
foo();
35+
} catch (e) {
36+
Sentry.captureException(e);
37+
}
38+
}, 99);
39+
},
40+
);
541
}
642
```
43+
44+
</OnboardingOption>

0 commit comments

Comments
 (0)