Skip to content

Commit

Permalink
feat(nestjs): Update nestjs error monitoring setup (#11141)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicohrubec authored Aug 27, 2024
1 parent c8fa1e0 commit 83fcfb6
Showing 1 changed file with 40 additions and 3 deletions.
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

0 comments on commit 83fcfb6

Please sign in to comment.