Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nestjs): Update nestjs error monitoring setup #11141

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions platform-includes/getting-started-use/javascript.nestjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ async function bootstrap() {
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>
Afterwards, add the `SentryModule` as a root module to your main module:

```javascript {filename: app.module.ts} {2, 8}
import { Module } from '@nestjs/common';
Expand All @@ -35,6 +33,45 @@ import { AppService } from './app.service';
export class AppModule {}
```

In case you are 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 `@WithSentry()` decorator to the `catch()` method of this global error filter. This decorator
will report all unexpected errors that are received by your global error filter to Sentry:

```javascript {2, 6}
import { Catch, ExceptionFilter } from '@nestjs/common';
import { WithSentry } from '@sentry/nestjs';

@Catch()
export class YourCatchAllExceptionFilter implements ExceptionFilter {
@WithSentry()
catch(exception, host): void {
// your implementation here
}
}
```

In case you do not have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main
module. This filter will report all unhandled errors to Sentry that are not caught by any other error filter.
**Important:** The `SentryGlobalFilter` needs to be registered before any other exception filters.

```javascript {3, 9}
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { SentryGlobalFilter } from '@sentry/nestjs/setup';

@Module({
providers: [
{
provide: APP_FILTER,
useClass: SentryGlobalFilter,
},
// ..other providers
],
})
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 {9}
Expand Down
Loading