Skip to content

Commit 5be0bfb

Browse files
committed
ref(nestjs): clarify exception predicate name
1 parent 31bc079 commit 5be0bfb

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

packages/nestjs/src/helpers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,20 @@ export function isExpectedError(exception: unknown): boolean {
2525
return true;
2626
}
2727

28-
// RpcException / WsException (same duck-type shape)
29-
if (isWsException(exception)) {
28+
if (isWsOrRpcException(exception)) {
3029
return true;
3130
}
3231

3332
return false;
3433
}
3534

3635
/**
37-
* Determines if the exception is a WsException (or RpcException, which has the same shape).
36+
* Determines if the exception is a WsException or RpcException, which have the same shape.
3837
* Both have `getError()` and `initMessage()` methods.
3938
*
4039
* We use duck-typing to avoid importing from `@nestjs/websockets` or `@nestjs/microservices`.
4140
*/
42-
export function isWsException(exception: unknown): boolean {
41+
export function isWsOrRpcException(exception: unknown): boolean {
4342
if (typeof exception !== 'object' || exception === null) {
4443
return false;
4544
}

packages/nestjs/src/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Catch, Global, HttpException, Injectable, Logger, Module } from '@nestj
1010
import { APP_INTERCEPTOR, BaseExceptionFilter } from '@nestjs/core';
1111
import { captureException, debug, getDefaultIsolationScope, getIsolationScope } from '@sentry/core';
1212
import type { Observable } from 'rxjs';
13-
import { isExpectedError, isWsException } from './helpers';
13+
import { isExpectedError, isWsOrRpcException } from './helpers';
1414

1515
// Partial extract of FastifyRequest interface
1616
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
@@ -164,7 +164,7 @@ class SentryGlobalFilter extends BaseExceptionFilter {
164164

165165
const client = host.switchToWs().getClient<{ emit?: (event: string, data: unknown) => void }>();
166166

167-
if (isWsException(exception)) {
167+
if (isWsOrRpcException(exception)) {
168168
const result = (exception as { getError: () => unknown }).getError();
169169
const response = typeof result === 'object' && result !== null ? result : { status: 'error', message: result };
170170
client.emit?.('exception', response);

packages/nestjs/test/sentry-global-filter.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SentryGlobalFilter } from '../src/setup';
88

99
vi.mock('../src/helpers', () => ({
1010
isExpectedError: vi.fn(),
11-
isWsException: vi.fn(),
11+
isWsOrRpcException: vi.fn(),
1212
}));
1313

1414
vi.mock('@sentry/core', () => ({
@@ -28,7 +28,7 @@ describe('SentryGlobalFilter', () => {
2828
let mockLoggerError: any;
2929
let mockLoggerWarn: any;
3030
let isExpectedErrorMock: any;
31-
let isWsExceptionMock: any;
31+
let isWsOrRpcExceptionMock: any;
3232

3333
beforeEach(() => {
3434
vi.clearAllMocks();
@@ -59,7 +59,7 @@ describe('SentryGlobalFilter', () => {
5959
mockCaptureException = vi.spyOn(SentryCore, 'captureException').mockReturnValue('mock-event-id');
6060

6161
isExpectedErrorMock = vi.mocked(Helpers.isExpectedError).mockImplementation(() => false);
62-
isWsExceptionMock = vi.mocked(Helpers.isWsException).mockImplementation(() => false);
62+
isWsOrRpcExceptionMock = vi.mocked(Helpers.isWsOrRpcException).mockImplementation(() => false);
6363
});
6464

6565
describe('HTTP context', () => {
@@ -274,7 +274,7 @@ describe('SentryGlobalFilter', () => {
274274

275275
it('does not capture expected WebSocket exceptions and emits their response', () => {
276276
isExpectedErrorMock.mockReturnValueOnce(true);
277-
isWsExceptionMock.mockReturnValueOnce(true);
277+
isWsOrRpcExceptionMock.mockReturnValueOnce(true);
278278
const exception = {
279279
getError: () => 'Expected WebSocket exception',
280280
initMessage: vi.fn(),

0 commit comments

Comments
 (0)