Skip to content

Commit 17472be

Browse files
committed
create and use new platform include for capturing error content
1 parent bd7fea7 commit 17472be

File tree

3 files changed

+87
-84
lines changed

3 files changed

+87
-84
lines changed

docs/platforms/javascript/guides/nestjs/index.mdx

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -41,90 +41,7 @@ To import and initialize Sentry, create a file named `instrument.ts` in the root
4141

4242
## Step 3: Capture Nest.js Errors
4343

44-
By default, Sentry only captures unhandled exceptions that aren't caught by an error filter.
45-
Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) aren't captured by default because they mostly act as control flow vehicles.
46-
47-
To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions:
48-
49-
### Using a Global Catch-All Exception Filter
50-
51-
If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method:
52-
53-
```typescript {2, 6}
54-
import { Catch, ExceptionFilter } from "@nestjs/common";
55-
import { SentryExceptionCaptured } from "@sentry/nestjs";
56-
57-
@Catch()
58-
export class YourCatchAllExceptionFilter implements ExceptionFilter {
59-
@SentryExceptionCaptured()
60-
catch(exception, host): void {
61-
// your implementation here
62-
}
63-
}
64-
```
65-
66-
### Not Using a Global Catch-All Exception Filter
67-
68-
If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters:
69-
70-
```typescript {2-3, 7-10}
71-
import { Module } from "@nestjs/common";
72-
import { APP_FILTER } from "@nestjs/core";
73-
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
74-
75-
@Module({
76-
providers: [
77-
{
78-
provide: APP_FILTER,
79-
useClass: SentryGlobalFilter,
80-
},
81-
// ..other providers
82-
],
83-
})
84-
export class AppModule {}
85-
```
86-
87-
### Using Error Filters for Specific Exception Types
88-
89-
If you have error filters for specific types of exceptions (for example, `@Catch(HttpException)`) and you want to report these errors to Sentry, you need to capture them in the `catch()` handler using `Sentry.captureException()`:
90-
91-
```typescript {4,9}
92-
import { ArgumentsHost, BadRequestException, Catch } from "@nestjs/common";
93-
import { BaseExceptionFilter } from "@nestjs/core";
94-
import { ExampleException } from "./example.exception";
95-
import * as Sentry from "@sentry/nestjs";
96-
97-
@Catch(ExampleException)
98-
export class ExampleExceptionFilter extends BaseExceptionFilter {
99-
catch(exception: unknown, host: ArgumentsHost) {
100-
Sentry.captureException(exception);
101-
return super.catch(new BadRequestException(exception.message), host);
102-
}
103-
}
104-
```
105-
106-
<Expandable title="Are you using Microservices?">
107-
If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see [Nest.js Microservices documentation](https://docs.nestjs.com/microservices/exception-filters)).
108-
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
109-
110-
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
111-
112-
```typescript
113-
import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common";
114-
import { Observable, throwError } from "rxjs";
115-
import { RpcException } from "@nestjs/microservices";
116-
import * as Sentry from "@sentry/nestjs";
117-
118-
@Catch(RpcException)
119-
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
120-
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
121-
Sentry.captureException(exception); // optional
122-
return throwError(() => exception.getError());
123-
}
124-
}
125-
```
126-
127-
</Expandable>
44+
<PlatformContent includePath="getting-started-capture-errors" />
12845

12946
## Step 4: Add Readable Stack Traces With Source Maps (Optional)
13047

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
By default, Sentry only captures unhandled exceptions that aren't caught by an error filter.
2+
Additionally, `HttpException`s (including [derivatives](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)) aren't captured by default because they mostly act as control flow vehicles.
3+
4+
To make sure Sentry captures all your app's errors, configure error handling based on how your application manages exceptions:
5+
6+
### Using a Global Catch-All Exception Filter
7+
8+
If you have a global catch-all exception filter, add a `@SentryExceptionCaptured()` decorator to the filter's `catch()` method:
9+
10+
```typescript {2, 6}
11+
import { Catch, ExceptionFilter } from "@nestjs/common";
12+
import { SentryExceptionCaptured } from "@sentry/nestjs";
13+
14+
@Catch()
15+
export class YourCatchAllExceptionFilter implements ExceptionFilter {
16+
@SentryExceptionCaptured()
17+
catch(exception, host): void {
18+
// your implementation here
19+
}
20+
}
21+
```
22+
23+
### Not Using a Global Catch-All Exception Filter
24+
25+
If you don't have a global catch-all exception filter, add the `SentryGlobalFilter` to the providers of your main module, **before** any other exception filters:
26+
27+
```typescript {2-3, 7-10}
28+
import { Module } from "@nestjs/common";
29+
import { APP_FILTER } from "@nestjs/core";
30+
import { SentryGlobalFilter } from "@sentry/nestjs/setup";
31+
32+
@Module({
33+
providers: [
34+
{
35+
provide: APP_FILTER,
36+
useClass: SentryGlobalFilter,
37+
},
38+
// ..other providers
39+
],
40+
})
41+
export class AppModule {}
42+
```
43+
44+
### Using Error Filters for Specific Exception Types
45+
46+
If you have error filters for specific types of exceptions (for example, `@Catch(HttpException)`) and you want to report these errors to Sentry, you need to capture them in the `catch()` handler using `Sentry.captureException()`:
47+
48+
```typescript {4,9}
49+
import { ArgumentsHost, BadRequestException, Catch } from "@nestjs/common";
50+
import { BaseExceptionFilter } from "@nestjs/core";
51+
import { ExampleException } from "./example.exception";
52+
import * as Sentry from "@sentry/nestjs";
53+
54+
@Catch(ExampleException)
55+
export class ExampleExceptionFilter extends BaseExceptionFilter {
56+
catch(exception: unknown, host: ArgumentsHost) {
57+
Sentry.captureException(exception);
58+
return super.catch(new BadRequestException(exception.message), host);
59+
}
60+
}
61+
```
62+
63+
<Expandable title="Are you using Microservices?">
64+
If you're using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see [Nest.js Microservices documentation](https://docs.nestjs.com/microservices/exception-filters)).
65+
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) doesn't extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
66+
67+
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
68+
69+
```typescript
70+
import { Catch, RpcExceptionFilter, ArgumentsHost } from "@nestjs/common";
71+
import { Observable, throwError } from "rxjs";
72+
import { RpcException } from "@nestjs/microservices";
73+
import * as Sentry from "@sentry/nestjs";
74+
75+
@Catch(RpcException)
76+
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
77+
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
78+
Sentry.captureException(exception); // optional
79+
return throwError(() => exception.getError());
80+
}
81+
}
82+
```
83+
84+
</Expandable>

platform-includes/getting-started-use/javascript.nestjs.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ import { AppService } from "./app.service";
3434
})
3535
export class AppModule {}
3636
```
37+
38+
<PlatformContent includePath="getting-started-capture-errors" />

0 commit comments

Comments
 (0)