-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(nestjs): Update nestjs sdk docs with new setup (#10784)
- Loading branch information
1 parent
52bd0b4
commit 71a4453
Showing
1 changed file
with
40 additions
and
11 deletions.
There are no files selected for viewing
51 changes: 40 additions & 11 deletions
51
platform-includes/getting-started-use/javascript.nestjs.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
``` |