Skip to content

Commit f0cdae5

Browse files
authored
docs(js): Add section on microservices for error handling (#13389)
1 parent 3702c8a commit f0cdae5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ import { SentryGlobalFilter } from "@sentry/nestjs/setup";
7373
export class AppModule {}
7474
```
7575

76+
<Expandable title="Using Microservices?">
77+
78+
If you are using `@nestjs/microservices` make sure to handle errors in RPC contexts correctly by providing your own `RpcExceptionFilter` (see https://docs.nestjs.com/microservices/exception-filters).
79+
`SentryGlobalFilter` in a [hybrid application](https://docs.nestjs.com/faq/hybrid-application) does not extend `BaseRpcExceptionFilter` since this depends on `@nestjs/microservices`.
80+
81+
Use `Sentry.captureException(exception)` in your custom filter in case you want to send these errors to Sentry:
82+
83+
```typescript
84+
import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common';
85+
import { Observable, throwError } from 'rxjs';
86+
import { RpcException } from '@nestjs/microservices';
87+
import * as Sentry from '@sentry/nestjs';
88+
89+
@Catch(RpcException)
90+
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
91+
catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
92+
Sentry.captureException(exception); // optional
93+
return throwError(() => exception.getError());
94+
}
95+
}
96+
```
97+
98+
99+
</Expandable>
100+
101+
76102
If you have error filters for specific types of exceptions (for example `@Catch(HttpException)`, or any other `@Catch(...)` with arguments) and you want to capture errors caught by these filters, capture the errors in the `catch()` handler with `Sentry.captureException()`:
77103

78104
```javascript {9}

0 commit comments

Comments
 (0)