Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/nestjs/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Determines if the exception is an expected NestJS control flow error.
* - HttpException have getStatus and getResponse methods: https://github.com/nestjs/nest/blob/master/packages/microservices/exceptions/rpc-exception.ts
* - RpcException have getError and initMessage methods: https://github.com/nestjs/nest/blob/master/packages/common/exceptions/http.exception.ts
* - WsException has getError and initMessage in current Nest versions, but we also accept a WsException-shaped fallback
* identified by constructor name to avoid reporting control-flow websocket exceptions.
*
* We cannot use `instanceof HttpException` here because this file is imported
* from the main entry point (via decorators.ts), and importing @nestjs/common at that
Expand All @@ -25,10 +27,15 @@ export function isExpectedError(exception: unknown): boolean {
return true;
}

// RpcException
// RpcException / WsException (current Nest versions)
if (typeof ex.getError === 'function' && typeof ex.initMessage === 'function') {
return true;
}

// WsException fallback (older/custom variants may not expose initMessage)
if (typeof ex.getError === 'function' && ex.constructor?.name === 'WsException') {
Copy link
Member

@nicohrubec nicohrubec Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think we need this? Did you find any old version specifically that would require this fallback? Else we will likely close this, because it seems to be already covered by our current logic since WsException and RpcException expose basically the same interface.

return true;
}

return false;
}
8 changes: 8 additions & 0 deletions packages/nestjs/test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe('isExpectedError', () => {
expect(isExpectedError(rpcLike)).toBe(true);
});

it('should return true for WsException-like objects without initMessage', () => {
const wsLike = {
getError: () => 'some error',
constructor: { name: 'WsException' },
};
expect(isExpectedError(wsLike)).toBe(true);
});

it('should return false for plain Error', () => {
expect(isExpectedError(new Error('test'))).toBe(false);
});
Expand Down
Loading