|
1 |
| -```javascript {filename: main.ts} {17} |
| 1 | +```javascript {filename: main.ts} |
2 | 2 | // Import this first!
|
3 | 3 | import './instrument';
|
4 | 4 |
|
5 | 5 | // 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'; |
12 | 7 | import { AppModule } from './app.module';
|
13 | 8 |
|
14 | 9 | async function bootstrap() {
|
15 | 10 | const app = await NestFactory.create(AppModule);
|
16 |
| - const { httpAdapter } = app.get(HttpAdapterHost); |
17 |
| - |
18 |
| - Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter)); |
19 |
| - |
20 | 11 | await app.listen(3000);
|
21 | 12 | }
|
22 | 13 |
|
23 | 14 | bootstrap();
|
24 | 15 | ```
|
| 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