Skip to content

Not throwing the exception/error when using ws adapter websocket #9056

Open
@SairamPotta

Description

@SairamPotta

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When I am trying to throw the exception/error on conditional basis on websocket it is not sending any handshake to the client.

here is the simple code

import WebSocket from 'ws';
import {
  SubscribeMessage,
  WebSocketGateway,
  WsException,
} from '@nestjs/websockets';

@WebSocketGateway({ path: '/api' })
export class MainGateway {
  @SubscribeMessage('message')
  handleMessage(client: WebSocket, payload: any) {
    if (payload.id === 4) {
      throw new WsException('not Found');
    }

    client.send(JSON.stringify({ id: payload.id }));
  }
}

I have just investigated why not throwing error found issue in this file
https://github.com/nestjs/nest/blob/master/packages/websockets/exceptions/base-ws-exception-filter.ts#L33
https://github.com/nestjs/nest/blob/master/packages/websockets/exceptions/base-ws-exception-filter.ts#L41

For ws - websocket adapter emit method is not working.

Minimum reproduction code

https://codesandbox.io/s/admiring-hugle-o8wqf?file=/src/Event/event.gateway.ts

Steps to reproduce

No response

Expected behavior

Should throw the handshake error or close the web socket.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

"@nestjs/common": "^8.0.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/platform-ws": "^8.2.6",
    "@nestjs/websockets": "^8.2.6",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "uuid": "^8.3.2",
    "ws": "^8.4.2"

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions