Skip to content

Commit 833a5a2

Browse files
committed
fix: excepting snippets
1 parent b3dc1a4 commit 833a5a2

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

content/recipes/sentry.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Sentry
22

3-
[Sentry](https://sentry.io) is an error tracking and performance monitoring platform that helps developers identify and fix issues in real-time. This recipe shows how to integrate Sentry with your NestJS application.
3+
[Sentry](https://sentry.io) is an error tracking and performance monitoring platform that helps developers identify and fix issues in real-time. This recipe shows how to integrate Sentry's [NestJS SDK](https://docs.sentry.io/platforms/javascript/guides/nestjs/) with your NestJS application.
44

55
#### Installation
66

@@ -67,7 +67,7 @@ bootstrap();
6767
Afterward, add the `SentryModule` as a root module to your main module:
6868

6969

70-
```typescript
70+
```typescript {2, 8}
7171
@@filename(app.module)
7272
import { Module } from "@nestjs/common";
7373
import { SentryModule } from "@sentry/nestjs/setup";
@@ -85,6 +85,47 @@ import { AppService } from "./app.service";
8585
export class AppModule {}
8686
```
8787

88+
#### Exception Handling
89+
90+
If you're 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 `@SentryExceptionCaptured()` decorator to the filter's `catch()` method. This decorator will report all unexpected errors that are received by your global error filter to Sentry:
91+
92+
```typescript {2, 6}
93+
import { Catch, ExceptionFilter } from '@nestjs/common';
94+
import { SentryExceptionCaptured } from '@sentry/nestjs';
95+
96+
@Catch()
97+
export class YourCatchAllExceptionFilter implements ExceptionFilter {
98+
@SentryExceptionCaptured()
99+
catch(exception, host): void {
100+
// your implementation here
101+
}
102+
}
103+
```
104+
105+
By default, only unhandled exceptions that are not caught by an error filter are reported to Sentry. `HttpExceptions` (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) are also not captured by default because they mostly act as control flow vehicles.
106+
107+
If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module. This filter will report any unhandled errors that aren't caught by other error filters to Sentry.
108+
109+
> warning **Important** The `SentryGlobalFilter` needs to be registered before any other exception filters.
110+
111+
```typescript {3, 9}
112+
@@filename(app.module)
113+
import { Module } from "@nestjs/common";
114+
import { APP_FILTER } from "@nestjs/core";
115+
+import { SentryGlobalFilter } from "@sentry/nestjs/setup";
116+
117+
@Module({
118+
providers: [
119+
{
120+
provide: APP_FILTER,
121+
useClass: SentryGlobalFilter,
122+
},
123+
// ..other providers
124+
],
125+
})
126+
export class AppModule {}
127+
```
128+
88129
#### Add Readable Stack Traces to Errors
89130

90131
Depending on how you've set up your project, the stack traces in your Sentry errors probably won't look like your actual code.

0 commit comments

Comments
 (0)