Skip to content

Commit

Permalink
docs(nestjs): Update nestjs sdk docs with new setup (#10784)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicohrubec authored Jul 24, 2024
1 parent 52bd0b4 commit 71a4453
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions platform-includes/getting-started-use/javascript.nestjs.mdx
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
```javascript {filename: main.ts} {17}
```javascript {filename: main.ts}
// Import this first!
import './instrument';

// Now import other modules
import * as Sentry from "@sentry/nestjs";
import {
BaseExceptionFilter,
HttpAdapterHost,
NestFactory
} from '@nestjs/core';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const { httpAdapter } = app.get(HttpAdapterHost);

Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));

await app.listen(3000);
}

bootstrap();
```

Then you can add the SentryModule as a root module:

<Note>The SentryModule needs to be registered before any other module that should be instrumented by Sentry.</Note>

```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 {}
```

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()`:

```javascript {11}
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)
}
}
```

0 comments on commit 71a4453

Please sign in to comment.