Skip to content

Commit 71a4453

Browse files
authored
docs(nestjs): Update nestjs sdk docs with new setup (#10784)
1 parent 52bd0b4 commit 71a4453

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed
Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
1-
```javascript {filename: main.ts} {17}
1+
```javascript {filename: main.ts}
22
// Import this first!
33
import './instrument';
44

55
// Now import other modules
6-
import * as Sentry from "@sentry/nestjs";
7-
import {
8-
BaseExceptionFilter,
9-
HttpAdapterHost,
10-
NestFactory
11-
} from '@nestjs/core';
6+
import { NestFactory } from '@nestjs/core';
127
import { AppModule } from './app.module';
138

149
async function bootstrap() {
1510
const app = await NestFactory.create(AppModule);
16-
const { httpAdapter } = app.get(HttpAdapterHost);
17-
18-
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
19-
2011
await app.listen(3000);
2112
}
2213

2314
bootstrap();
2415
```
16+
17+
Then you can add the SentryModule as a root module:
18+
19+
<Note>The SentryModule needs to be registered before any other module that should be instrumented by Sentry.</Note>
20+
21+
```javascript {filename: app.module.ts} {2, 8}
22+
import { Module } from '@nestjs/common';
23+
import { SentryModule } from '@sentry/nestjs/setup';
24+
import { AppController } from './app.controller';
25+
import { AppService } from './app.service';
26+
27+
@Module({
28+
imports: [
29+
SentryModule.forRoot(),
30+
// ...other modules
31+
],
32+
controllers: [AppController],
33+
providers: [AppService],
34+
})
35+
export class AppModule {}
36+
```
37+
38+
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()`:
39+
40+
```javascript {11}
41+
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common';
42+
import { BaseExceptionFilter } from '@nestjs/core';
43+
import { ExampleException } from './example.exception';
44+
import * as Sentry from '@sentry/nestjs';
45+
46+
@Catch(ExampleException)
47+
export class ExampleExceptionFilter extends BaseExceptionFilter {
48+
catch(exception: unknown, host: ArgumentsHost) {
49+
Sentry.captureException(exception);
50+
return super.catch(new BadRequestException(exception.message), host)
51+
}
52+
}
53+
```

0 commit comments

Comments
 (0)