how to send the exception/error for nestjs websocket of adapter @nestjs/platform-ws

I am trying to send the exception using nestjs websocket based on conditions, tried using

throw new WsException('Invalid data');

but not sending any exception

Here is the sample code

import WebSocket from 'ws';
import {
} from '@nestjs/websockets';

@WebSocketGateway({ path: '/api' })
export class MainGateway {
  handleMessage(client: WebSocket, payload: any) {
    if ( === 4) {
      throw new WsException('Invalid Data');
    client.send(JSON.stringify({ id: }));

and I'm creating the connection using angular here is the code snippet

export class WsComponent implements OnInit {
  public value!: number;
  public subject$ = webSocket('ws://localhost:3000/api');

  ngOnInit(): void {
    const event = { event: 'message', data: { id: 4 } };

      next: (v: any) => (this.value =,
      error: (e) => console.error(e),
      complete: () =>'complete'),


Please help me to solve the issue


  • I have struggled with same issue. I believe the issus is with BaseWsExceptionFilter and its usage of soket.emit. I've come up with following:

    import { ArgumentsHost, Catch, HttpException } from "@nestjs/common";
    import { BaseWsExceptionFilter, WsException } from "@nestjs/websockets";
    @Catch(WsException, HttpException)
    export class WebsocketExceptionsFilter extends BaseWsExceptionFilter {
      catch(exception: WsException | HttpException, host: ArgumentsHost) {
        const client = host.switchToWs().getClient() as WebSocket;
        const data = host.switchToWs().getData();
        const error = exception instanceof WsException ? exception.getError() : exception.getResponse();
        const details = error instanceof Object ? { ...error } : { message: error };
          event: "error",
          data: {
            id: (client as any).id,
            rid: data.rid,

    Pretty much sure extends BaseWsExceptionFilter is redundant as I did't use anything of that class. And then I applied it to my gateway:

    @UsePipes(new ValidationPipe({ transform: true }))
    export class FeedGateway implements OnGatewayConnection, OnGatewayDisconnect {

    This helped me to receive following error:

    {"event":"error","data":{"id":"7a784ce568767a1016090c6a","rid":"connect","statusCode":400,"message":["language must be a valid enum value"],"error":"Bad Request"}}