Sairam
Sairam

Reputation: 63

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 {
  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('Invalid Data');
    }
    client.send(JSON.stringify({ id: payload.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 } };

    this.subject$.subscribe({
      next: (v: any) => (this.value = v.id),
      error: (e) => console.error(e),
      complete: () => console.info('complete'),
    });

    this.subject$.next(event);
  }
}

Please help me to solve the issue

Upvotes: 3

Views: 7888

Answers (4)

blid
blid

Reputation: 1171

None of the answers worked for me. I had to call client.emit, not client.send, to get it working:

In controller:

import { ArgumentsHost, Catch, HttpException } from '@nestjs/common';
import { WsException } from '@nestjs/websockets';

@Catch(WsException)
export class WsExceptionFilter {
  // Or any other exception.
  catch(exception: WsException, host: ArgumentsHost) {
    const client = host.switchToWs().getClient();
    client.emit('my-log-error-event', exception);
  }
}

and in the gateway:


@WebSocketGateway()
@UseFilters(new WsExceptionFilter())
export class ConnectionGateway {}

Upvotes: 0

Cytronic
Cytronic

Reputation: 213

The updated comment by @WilyWork is correct, but I found that in my case, the function is not the last argument.

I decided to come up with this simple function instead. It should do the job until a more robust solution comes up.

... in WsException filter
const args = host.getArgs();

        // Find possible acknowledgement callback from the end of arguments
        const ackCallback = this.findAckCallback(args);

        if (ackCallback !== null) {
            console.log("acknowledgement callback exists");
            ackCallback(wsEventErrorResponse);
        } else {
            console.log("acknowledgement callback does not exist");
            client.emit("globalError", wsEventErrorResponse);
        }
    }

    /**
     * Finds the acknowledgement callback from the end of the arguments.
     * @param args The arguments passed to the event handler.
     * @returns The acknowledgement callback if it exists, otherwise null.
     */
    private findAckCallback(args: unknown[]): Function | null {
        if (Array.isArray(args) && args.length >= 1) {
            for (let i = args.length - 1; i >= Math.max(0, args.length - 3); i--) {
                const arg = args[i];
                if (typeof arg === "function") {
                    return arg;
                }
            }
        }
        return null;
    }

Upvotes: 2

WilyWork
WilyWork

Reputation: 33

it's working but needs more testing. If you find a bug, please report it.

import { ArgumentsHost, Catch } from '@nestjs/common';
import { BaseWsExceptionFilter } from '@nestjs/websockets';
import { PacketType } from 'socket.io-parser';
@Catch()
export class AllExceptionsSocketFilter extends BaseWsExceptionFilter {
   catch(exception: any, host: ArgumentsHost) {
      const client = host.switchToWs().getClient();
      client.packet({
          type: PacketType.ACK,
          data: [{ error: exception?.message }],
          id: client.nsp._ids++,
      });
   }
}

Use:

@WebSocketGateway()
@UseFilters(new AllExceptionsSocketFilter())
export class ContatoGateway {
   ...

EDIT: New method, working and tested:

import { ArgumentsHost, Catch } from '@nestjs/common';
import { BaseWsExceptionFilter } from '@nestjs/websockets';

@Catch()
export class AllExceptionsSocketFilter extends BaseWsExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
  const args = host.getArgs();
  // event ack callback
  if ('function' === typeof args[args.length - 1]) {
    const ACKCallback = args.pop();
    ACKCallback({ error: exception.message, exception });
  }
}

}

Upvotes: 3

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 };
    client.send(JSON.stringify({
      event: "error",
      data: {
        id: (client as any).id,
        rid: data.rid,
        ...details
      }
    }));
  }
}

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

@WebSocketGateway()
@UseFilters(WebsocketExceptionsFilter)
@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"}}

Upvotes: 4

Related Questions